1

I have writing some code of marquee in textview.

When I change the language from English to Arabic.

The text's direction will change from right to left(original is left to right in English), but the marquee's direction don't change, it always scroll from right to left.

My code as below

<TextView android:id="@+id/label" android:layout_width="@dimen/menu_item_txt_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:text="@string/empty" android:textColor="@drawable/menu_txt_color" android:textSize="@dimen/menu_item_txt_size" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:singleLine="true" />

Can anyone provide me simple way to fix it?

In the English the text direction is "Move to[ 888888888 ]" ---> left to right In the Arabic the text direction is "[ 888888888 ]نقل الى" right to left

But in the marquee,

In English version, when I not focus the textview, it will show "Move to[ 88", then I focus it will scroll and show "88888 ]" -----> right to left

In Arabic version, when I not focus the textview, it will show "88 ]نقل الى", then I focus it will scroll and show "نقل " -----> right to left

1 Answers1

-1

There's no native way of achieving that, i.e., there's not a built-in method that will do what you want, but there's a workaround to make it work that way. Wrap your TextView into a HorizontalScrollView and you'll need to control it with the position of the scrollbar. Firstly, get the scrollbar position with this:

scrollPos = (int) tv.getLayout().getLineWidth(0);

The next step is to define a Handler and call recursively until you reach the scroll limit, this should work:

Handler hsHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    // Being hsView your HorizontalScrollView
    hsView.scrollTo(scrollPos, 0);
    scrollPos--;
    if (scrollPos >= 0)
      hsHandler.sendEmptyMessage(0);
  }
};
nKn
  • 13,691
  • 9
  • 45
  • 62
  • there should be an comment not answer that you copy paste – Ammar ali Mar 03 '14 at 08:34
  • That's not a copy and paste, that's a solution I've used in one of my projects years ago and it still works. I actually copied that from my project. – nKn Mar 03 '14 at 08:37
  • Dear nKn, Thanks your reply, I have try this code in the past, but I always get NullPointerException in tv.getLayout(). – user3374004 Mar 04 '14 at 06:27
  • @user3374004 You're probably calling it too early, have a look at this http://stackoverflow.com/questions/16558948/how-to-use-textview-getlayout-it-returns-null – nKn Mar 04 '14 at 09:33
  • @nKn I had try this, and it could't occur exception, but it cannot work. Are width and height incorrect? – user3374004 Mar 04 '14 at 11:51