jack peterson
jack peterson

Reputation: 297

Reverse marquee of a textview

After trying different ways to reverse the marquee of a textview in Android. I used this "link" to do it.

but it doesn't work for me! what's wrong with my code? its the only way for me and I don't want Animation solutions cause of Screen limitations.

XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/hScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal" >

    <LinearLayout
        android:id="@+id/lyr"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="test for reverse marqueee a.test for reverse marqueee b.test for reverse marqueee c." />
    </LinearLayout>

</ScrollView>

</LinearLayout>

JAVA:

public class TextmovingActivity extends Activity {

      private Integer scroll_pos;
      Handler         hHandler;
      TextView        tv;
      LinearLayout    hsView;
      ScrollView      hScroll;



      @Override
      public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                tv = (TextView) findViewById(R.id.tv);

                hScroll = (ScrollView) findViewById(R.id.hScroll);
                tv.setSelected(true);
                hHandler = new Handler()
                {

                          @Override
                          public void handleMessage(Message msg)
                          {
                                    scroll_pos = (int) tv.getLayout().getLineWidth(0);
                                    hScroll.scrollTo(scroll_pos, 0);
                                    scroll_pos--;
                                    if (scroll_pos >= 0)
                                              hHandler.sendEmptyMessage(0);
                          }
                };
      }
  }

Upvotes: 4

Views: 1715

Answers (1)

jack peterson
jack peterson

Reputation: 297

this solution works for rtl languages (Arabic , Persian , ....) :

just add this attribute to your TextView:

android:textDirection="rtl"

for APIs under 17 add this attribute to your TextView:

android:gravity="right"

(it doesn't work with emulator but it works perfectly with the real phone.)

Examlpe:

XML:

    <TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="write something in Arabic,Persian,... here"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    />

JAVA:

TextView txt = (TextView) findViewById(R.id.txt);
txt.setSelected(true);

Upvotes: 2

Related Questions