Search code examples
androidtextviewarabicscrollable

Horizontally Scrollable TextView show inconsistent behaviour across android versions


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"

Solution

  • 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" />