How to Reverse the Direction of Marquee of a Textview

How to reverse the direction of marquee of a TextView

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!

Reverse marquee of a textview

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

How to make marquee text pop up again immediately after it leaves the screen view in Android

Take a textview in your layout file.

<TextView
android:id="@+id/txtMarquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white"
android:gravity="center"
android:layout_margin="0.50dp"
android:padding="5dp"
android:singleLine="true"
android:textColor="@color/bg_login"
android:textSize="20sp" />

Write below code in your MainActivity in OnCreate method.

TextView txtMarquee = (TextView) findViewById(R.id.txtMarquee);
Animation marquee = AnimationUtils.loadAnimation(this, R.anim.marquee);
txtMarquee.setShadowLayer(0,0,0,0);
txtMarquee.startAnimation(marquee);
txtMarquee.setText("message");

Get Notified after 1st rotation of Text in Text View(ellipsize=marquee) in Android

I made a custom marquee and when that marquee ends I can get a notification.

Here, I have answered my own question before this one, as these two question were related.

Android marquee backwards

Here is the relevant code in Android that handles the Marquee. It's pretty tightly coupled to the TextView class, so if you wanted to implement a custom marquee I believe you would need to write your own TextView.

Is string shown in marquee textview is ended in android?

Please learn this from following Stack overflow answers:

How to reverse the direction of marquee of a TextView

and

Fire event when marquee completes

Is there a way to make ellipsize=marquee always scroll?

I have been facing the problem and the shortest solution I have come up with is to create a new class derived from TextView.
The class should override three methods onFocusChanged, onWindowFocusChanged and isFocused to make the TextView all focused.

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}

@Override
public boolean isFocused() {
return true;
}


Related Topics



Leave a reply



Submit