How can I use marquee text in an android application?
-
Also, have a look at http://stackoverflow.com/questions/1827751/is-there-a-way-to-make-ellipsize-marquee-always-scroll – Felipe Oct 19 '11 at 17:59
-
Try alwaysMarqueeTextView stackoverflow.com/a/28806003/3496570 and forget about setSelection – Zar E Ahmer Mar 02 '15 at 09:11
-
**This answer can help you** [animate textView](https://stackoverflow.com/a/59223215/8748900) – abolfazl bazghandi Dec 07 '19 at 05:16
19 Answers
Here is an example:
public class TextViewMarquee extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true); // Set focus to the textview
}
}
The xml file with the textview:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/mywidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textColor="#ff4500"
android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>
- 2,241
- 2
- 17
- 31
- 6,628
- 8
- 30
- 27
-
2
-
1This code worked for me - although I had to make sure the android:textColor was set otherwise I was sometimes getting very dark text on my already dark background. This is apparently different from the non-scrolling default in my setup which was white text. – JWGS Apr 16 '12 at 14:34
-
1This will not work, in case when your text will be short instead of this long line. – Robi Kumar Tomar Jun 17 '14 at 09:27
-
1
-
For those who uses Anko, equivalent of `marquee_forever` is `marqueeRepeatLimit = 0xFFFFFFFF.toInt()` – Miha_x64 May 16 '17 at 15:48
-
-
for me it's working fine for large text, but for small text like "hello", it's not moving. can someone help me – AMIT Nov 14 '19 at 07:00
-
1Combining ellipsedsize marquee with maxlines 1 can lead to crashes, replace maxlines with singleline=true – Gastón Saillén Apr 01 '20 at 15:51
TextView textView = (TextView) this.findViewById(R.id.textview_marquee);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);
- 19,115
- 21
- 107
- 154
- 3,461
- 6
- 31
- 45
android:ellipsize="marquee"
This only works when your TextView has the focus.
- 28,933
- 23
- 88
- 140
- 9,495
- 2
- 33
- 53
-
He just did. You set `android:ellipsize="marquee"` on your `TextView`. It's all documented on developer.android.com by the way. – Matthias Feb 02 '10 at 12:17
-
-
for me it's working fine for large text, but for small text like "hello", it's not moving. can someone help me – AMIT Nov 14 '19 at 07:00
Just put these params to your TextView - It works :D
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
And you also need to setSelected(true):
my_TextView.setSelected(true);
Greetings, Christopher
- 5,590
- 1
- 18
- 28
- 1,350
- 14
- 18
-
1Yes after putting my_TextView.setSelected(true),It's working Perffect.....Thanks.... – Vatsal Harde May 16 '17 at 04:56
-
To get this to work, I had to use ALL three of the things (ellipsize, selected, and singleLine) mentioned already:
TextView tv = (TextView)findViewById(R.id.someTextView);
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true):
- 101
- 1
- 2
Xml code
<TextView
android:id="@+id/txtTicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:scrollHorizontally="true"
android:shadowColor="#FF0000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
Java
txtEventName.setSelected(true);
if text is small then add space before and after text
txtEventName.setText("\t \t \t \t \t \t"+eventName+"\t \t \t \t \t \t");
- 2,305
- 6
- 28
- 30
-
This helped me realise that I need the text to be longer than the width of the screen. – Leon Dec 03 '15 at 11:36
I found a Problem with marquee that it can't be used for short strings(As its function is only to Show Long strings).
I suggest Use Webview If you want to move short strings horizontally. Main_Activity.java Code:`
WebView webView;
webView = (WebView)findViewById(R.id.web);
String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
+ "Hello Droid" + "</marquee></FONT></html>";
webView.loadData(summary, "text/html", "utf-8"); // Set focus to the textview
`
main_activity.xml code:
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/web"
></WebView>
- 107
- 1
- 11
- 89
- 1
- 5
-
The most useful and flexible approach to implement a text ticker (running string). Thanks. – Dmitry Bilko Sep 04 '17 at 13:00
-
In XML file, you need to add following additional attributes in a TextView to get a marquee like feature:
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
In MainActivity.java file, you can get the reference of this TextView by using findViewById() and you can set the following property to this TextView to make it appear like a marquee text:
setSelected(true);
That's all you need.
- 659
- 8
- 10
I have tried all of the above, but for me its didn't work. When I add
android:clickable="true"
then it's worked perfectly for me. I don't know why. But I am happy to work it.
Here is my full answer.
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
- 1,651
- 1
- 14
- 17
Add Below Code in XML
<TextView
android:text="Shops NearBy textdf fsdgsdgsdg dsgtsgsdgsdgsg"
android:id="@+id/txtEventName"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:singleLine="true"/>
In Java add following Code:
TextView txtEventName=(TextView)findViewById(R.id.txtEventName);
txtEventName.setSelected(true);
- 109
- 10
As well as the XML settings identified by droidgren, my tests have shown that if the text you want to display is shorter than the width of the textview, then the marquee won't scroll at all. Possible solutions are to set the width of the view to a size smaller than the length of the text, or to concatenate the string to itself 2 or 3 times, with perhaps appropriate whitespace in-between so that the scrolling looks ok.
- 10,665
- 9
- 50
- 70
-
1I think you misunderstood the idea behind the marquee. It's not to make text move across the screen, it's to display text that is, well, longer than the width of the text view without having to shorten it. – Martin Marconcini Jul 12 '13 at 19:34
-
1You're right. I did misunderstand it. Thanks for clearing that up. – John J Smith Jul 13 '13 at 10:08
Use this to set Marque:
final TextView tx = (TextView) findViewById(R.id.textView1);
tx.setEllipsize(TruncateAt.MARQUEE);
tx.setSelected(true);
tx.setSingleLine(true);
tx.setText("Marquee needs only three things to make it run and these three things are mentioned above.");
You do not need to use "android:marqueeRepeatLimit="marquee_forever" into xml file. Marquee will work even without this.
- 108
- 1
- 10
Lots of answers correctly state that calling textView.setSelected(true) is required. However, if preferred to do so just via XML, one option could be to make use of the Binding Adapters when working with Data Binding.
So, just create a new adapter similar to:
@BindingAdapter("app:autoStartMarquee")
fun setAutoStartMarquee(textView: TextView, autoStartMarquee: Boolean) {
textView.isSelected = autoStartMarquee
}
Then, you can simply use it in the XML as follows:
...
<TextView
...
android:ellipsize="marquee"
android:singleLine="true"
app:autoStartMarquee="@{true}"/>
...
No need to call it from code anymore.
- 984
- 8
- 20
You can use a TextView or your custom TextView. The latter is when the textview cannot get focus all the time.
First, you can use a TextView or a custom TextView as the scrolling text view in your layout .xml file like this:
<com.example.myapplication.CustomTextView
android:id="@+id/tvScrollingMessage"
android:text="@string/scrolling_message_main_wish_list"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/black"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:freezesText="true"/>
NOTE: in the above code snippet com.example.myapplication is an example package name and should be replaced by your own package name.
Then in case of using CustomTextView, you should define the CustomTextView class:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@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;
}
}
Hope it will be helpful to you. Cheers!
- 381
- 3
- 11
With the above answer, you cannot set the speed or have flexibility for customizing the text view functionality. To have your own scroll speed and flexibility to customize marquee properties, use the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:lines="1"
android:id="@+id/myTextView"
android:padding="4dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Simple application that shows how to use marquee, with a long text" />
Within your activity:
private void setTranslation() {
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, 0.0f);
tanim.setDuration(1000);//set the duration
tanim.setInterpolator(new LinearInterpolator());
tanim.setRepeatCount(Animation.INFINITE);
tanim.setRepeatMode(Animation.ABSOLUTE);
textView.startAnimation(tanim);
}
This will be equivalent to "end":
where = TruncateAt.END
- 81,190
- 27
- 202
- 228
- 1
This is my xml customTextView Object here you can use simply TextView to replace on Tag.
<com.wedoapps.crickethisabkitab.utils.view.montserrat.CustomTextView
android:id="@+id/lblRateUsPlayStore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_10sdp"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_5sdp"
android:text="@string/please_rate_us_5_star_on_play_store"
android:textAllCaps="false"
android:textColor="@color/green"
android:textSize="@dimen/_25ssp"
android:textStyle="bold"
android:visibility="visible"
android:linksClickable="true"
android:autoLink="web|phone"/>
And here is My Java File code. i have set my html text on server just replace your text on textview object. i have put this code is marquee tag with clickable if any links on this textview to open mobile or webBrowser.
CustomTextView lblRateUsPlayStore = findViewById(R.id.lblRateUsPlayStore);
lblRateUsPlayStore.setMovementMethod(LinkMovementMethod.getInstance());
lblRateUsPlayStore.setText( Html.fromHtml(documentSnapshot.getString("DisplayText")));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(lblRateUsPlayStore, 12, 20, 2, 1);
lblRateUsPlayStore.setEllipsize(TextUtils.TruncateAt.MARQUEE);
// Set marquee repeat limit (unlimited)
lblRateUsPlayStore.setMarqueeRepeatLimit(-1);
lblRateUsPlayStore.setHorizontallyScrolling(true);
lblRateUsPlayStore.setSelected(true);
lblRateUsPlayStore.setLinksClickable(true);
lblRateUsPlayStore.setFocusableInTouchMode(true);
lblRateUsPlayStore.setFocusable(true);
- 39
- 1
- 7
You can use
android:ellipsize="marquee"
with your textview.
But remember to put focus on the desired textview.
- 388
- 4
- 12
For setting Marquee programatically
TextView textView = (TextView) this.findViewById(R.id.textview_marquee);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(-1);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);
- 61
- 4