Question

I have a text view that has a single lined Arabic (Right-to-Left) text. The Text content increases by time, and the last (left-most) part should always appear. The horizontal scroll bar is initially set to right, and auto scrolls to left as text increases.

The below code works great on a 2.3 Android phone. This comes from the layout.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="none"
    android:fadingEdge="horizontal"
    android:gravity="right"
    android:scrollHorizontally="true"
    android:scrollbars="horizontal"
    android:singleLine="true"
    android:text="@string/txt_start"
    android:textAppearance="?android:attr/textAppearanceLarge" />

I have this in the onCreate() method of my Activity

tv = (TextView) findViewById(R.id.textView1);
tv.setMovementMethod(new ScrollingMovementMethod());

The problem is the inconsistent behaviour of the text view on phones having android 4.0+ . The horizontal scroll bar is initially no placed on the right, so the TextView is initially blanked! If I try to scroll the blank textView, the text shows up. When extra text is added, the scroll-bar is not scrolled to show the new text, but if I manually scrolled the text, it appears.

I searched stackoverflow, and tried to add the below properties with no success.

android:focusable="true"
android:focusableInTouchMode="true"
Was it helpful?

Solution 3

It's found that the gravity Left/Right are deprecated, a developer should use Start/End. For the text to scroll it needs a "wrap_content" set to its width, additionally the textDirection needs to be set to RTL. For unknown reasons, textDirection did not work programmatically, but worked in the xml.

This code works well on all API versions of android.

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:textDirection="rtl"
        android:scrollHorizontally="true"
        android:scrollbars="horizontal"
        android:singleLine="true"
        android:ellipsize="none"
        android:text="@string/txt_start"
        android:textAppearance="?android:attr/textAppearanceLarge" />

OTHER TIPS

None of those solutions worked in my case where I wanted to animate scrolling of TextView with android:gravity="right". Simple workaround that helped though is to keep TextView gravity to left but place it aligned to right inside a RelativeLayout container:

<RelativeLayout
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <AutoScrollingTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="left"
    android:singleLine="true"
    android:ellipsize="none">
</RelativeLayout>

Try this example, it should work:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
         android:id="@+id/myText"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/myscroll"
         android:ellipsize="marquee"
         android:singleLine="true"
         android:scrollHorizontally="true"
         android:marqueeRepeatLimit="10"
>
</LinearLayout>

Note: when testing the marquee, don't test it in design view inside Eclipse, try it on the emulator\device ..

Also if you want to add a scrolling effect in code:

TextView tView = (TextView) findViewById(R.id.myText);
tView.stSelected(true);

To make the marquee scroll non-stopping on the end of marquee:

android:marqueeRepeatLimit=”marquee_forever”
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top