Khawar
Khawar

Reputation: 5227

How to reverse the direction of marquee of a TextView

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?

Upvotes: 12

Views: 9402

Answers (8)

Saeed Arianmanesh
Saeed Arianmanesh

Reputation: 1428

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>
    

Upvotes: 6

emad pirayesh
emad pirayesh

Reputation: 694

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

Upvotes: 0

ali shekari
ali shekari

Reputation: 740

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);

Upvotes: 0

Parag Chauhan
Parag Chauhan

Reputation: 35976

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);

Upvotes: 8

Khawar
Khawar

Reputation: 5227

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!

Upvotes: 10

einschnaehkeee
einschnaehkeee

Reputation: 1888

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. :-)

Upvotes: 1

Moog
Moog

Reputation: 10193

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).

Upvotes: 1

Pratik Sharma
Pratik Sharma

Reputation: 13415

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.

Upvotes: 0

Related Questions