12

I want to reverse the direction of marquee in the TextView. By default, the text moves from Right to Left, I want it to move from Left to Right. How can I do this?

Khawar
  • 5,197
  • 4
  • 28
  • 52

8 Answers8

10

I figured out a very simple and easy way to do this. I made a marquee effect to move in both directions depending on our selection. So, here is the trick:

I used a TextView inside a HorizontalScrollView. I controlled its scrolling in a programmatic way. I got length of text using:

scroll_pos = (int)myTextView.getLayout().getLineWidth(0);

Then I used Handler and called it recursively until I reach the scroll limit. In this handler I made my HorizontalScrollView to scroll to a certain position:

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

And here it goes, a smooth marquee from Left to Right. Cheers!

Khawar
  • 5,197
  • 4
  • 28
  • 52
  • Would you please explain more? for example what's `hScroll`? – boomz Apr 08 '13 at 14:21
  • @khawar could you please give me the links or references so that i could also do this. I need the same functionality. – newBie Jun 14 '13 at 09:12
  • 2
    @khawar -- It's giving "java.lang.NullPointerException" at line :scroll_pos = (int)myTextView.getLayout().getLineWidth(0); " Any idea regarding that ? – Harsh Trivedi Apr 28 '14 at 11:15
  • @HarshTrivedi: go for a global tree observer, you are getting layout too soon. – Behnam Apr 30 '14 at 12:01
8

Another simple way is using HTML and also you can change direction easily direction="Left"

<html><body><FONT COLOR="#000000" ><marquee id="mrqSlogan"  direction="Left" style="width: auto;" >text your</marquee></FONT></body></html>

And Pass to WebView

webView.loadDataWithBaseURL(null, yourhtmltext, "text/html" , null, null);
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
6

Finally I found the BEST Solution here: https://github.com/Torob/MarqueeView

to use this library:

  1. simply add this java file (MarqueeView.java) to your project java folder
  2. in your xml layout put your TextView inside of this MarqueeView like this:

      <your.packagename.MarqueeView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/horizontalScrollView"
           android:scrollbars="none"
           android:visibility="visible"
           android:clickable="false"> 
       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test"
            android:id="@+id/scrollingtext"
            android:singleLine="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingRight="25dp"
            android:paddingLeft="25dp" />
       </your.packagename.MarqueeView>
    
Saeed Arianmanesh
  • 1,269
  • 2
  • 22
  • 33
1

The functionality you are looking for does not appear to be available at this time.

You could create your own reverse marquee textview from the source in TextView.java but there are quite a few references to "marquee" within it. I counted more than 50 so it may take some time to reverse the scroll direction.

I thought some bi-directional language support might allow you to trick the textview into scrolling left to right but Android does not seem to support RTL languages very well.

For now your only option would be to accept the direction of the marquee or create your own TextView class that supports your functionality.

I would look at this section from line 3810 - 3815

  if (mMarquee != null && mMarquee.isRunning()) {
        canvas.translate(-mMarquee.mScroll, 0.0f);
    }

removing the minus sign before mMarquee becomes:

  if (mMarquee != null && mMarquee.isRunning()) {
        canvas.translate(mMarquee.mScroll, 0.0f);
    }

obvously you will need to make additional changes but this would point you in the right direction (literally).

Moog
  • 10,193
  • 2
  • 40
  • 66
1

I would suggest you create your own component with this behavior.

Like this: ViewFlipper with a single TextView as it's child (with your text displayed).

flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.marquee_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.marquee_out));

Marquee in:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"
        android:duration="1500"/>
</set>

Marquee out:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="1500"/>
</set>

You have to tweak a bit the duration to make it look like a "native" animation. :-)

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
  • Thanks. I am gonna try it and I'll let you know the progress. – Khawar Aug 08 '11 at 04:36
  • No problem. Keep in mind, that your flipInterval of your ViewFlipper should be equal to your summed up durations. So something like this: flipper.setFlipInterval(3000); flipper.startFlipping(); – einschnaehkeee Aug 08 '11 at 08:34
0

For right to left text for every one to use: try this:

        TextView tv = (TextView) findViewById(R.id.txt);
    Rect bounds = new Rect();
    Paint textPaint = tv.getPaint();
    String text = tv.getText().toString();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    int width = bounds.width();
    LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
    lp.width = width + 100;
            int startX = 300;
    TranslateAnimation ta = new TranslateAnimation(startX, lp.width, 0, 0);
    ta.setDuration(20000);
    ta.setRepeatCount(-1);
    tv.setAnimation(ta);
ali shekari
  • 740
  • 1
  • 11
  • 26
0

set layout direction android:layoutDirection="rtl" android:textDirection="rtl"

 <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_weight="1"
            android:layoutDirection="rtl"
            android:textDirection="rtl"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text=" ......... گپ دوستان بیسیبییبب "
            android:textColor="@color/colorPrimaryDark"
            android:textSize="@dimen/font_size_xlarge" />
emad pirayesh
  • 654
  • 7
  • 12
0

Here is the solution :

Set this in layout file :

<TextView
android:id="@+id/textId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:textColor="#337700"
android:textSize="20px"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:textStyle="bold"
android:text="Download Form for Different Vehiecle Purposes ....!!!">
</TextView>

And set this in activity :

    tv = (TextView) findViewById(R.id.textId);
    tv.setSelected(true);

In My case this is moving text from right to left.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • Sorry, earlier i wrote 'Default direction Left to Right' by mistake. Now I have edited my question. – Khawar Jul 15 '11 at 10:52