80

How can I use marquee text in an android application?

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
BIBEKRBARAL
  • 4,455
  • 9
  • 31
  • 30

21 Answers21

149

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>
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
droidgren
  • 6,898
  • 8
  • 31
  • 27
25
android:ellipsize="marquee"

This only works when your TextView has the focus.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Maurits Rijk
  • 9,789
  • 2
  • 36
  • 53
  • He just did. You set `android:ellipsize="marquee"` on your `TextView`. It's all documented on developer.android.com by the way. – mxk Feb 02 '10 at 12:17
  • only after getting the focus, the textview started the marquee effect. – Akshat May 02 '14 at 20:00
  • 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
25
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);
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Ashish Anand
  • 3,531
  • 6
  • 34
  • 45
18

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

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
Christopher Stock
  • 1,399
  • 15
  • 18
10

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):
BRG
  • 101
  • 1
  • 2
10

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");
PrvN
  • 2,385
  • 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
8

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>
gardenapple
  • 146
  • 1
  • 12
Akshay Upadhyay
  • 109
  • 1
  • 5
6

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.

Akshay Chopra
  • 1,035
  • 14
  • 10
2

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"
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
2

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);
Nikhil
  • 107
  • 10
2

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.

Sarquella
  • 1,129
  • 9
  • 26
2

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.

John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • 1
    I 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
  • 1
    You're right. I did misunderstand it. Thanks for clearing that up. – John J Smith Jul 13 '13 at 10:08
1

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.

Dhiraj Aggarwal
  • 118
  • 1
  • 10
1

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!

David Roman
  • 421
  • 4
  • 11
1

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);
    } 
cNgamba
  • 111
  • 1
  • 10
Jaydev
  • 1,794
  • 20
  • 37
0

This will be equivalent to "end":

where = TruncateAt.END
NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

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

You can use

android:ellipsize="marquee"

with your textview.

But remember to put focus on the desired textview.

Rohit Lalwani
  • 529
  • 7
  • 14
0

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

Special Thanks to David Roman , i changed his answer to kotlin

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

class MarqueeTextView : AppCompatTextView {
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
    context!!,
    attrs,
    defStyle
) {
}

override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
    if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect)
}

override fun onWindowFocusChanged(focused: Boolean) {
    if (focused) super.onWindowFocusChanged(focused)
}

override fun isFocused(): Boolean {
    return true
}
}
-1

In Android (KOTLIN), the Marquee effect for scrolling text can be achieved by using the TextViewwidget and setting the android:ellipsize and android:marqueeRepeatLimit attributes in the layout XML file. Here's an example of how to create a marquee TextView in Kotlin:

  1. In your layout XML file (e.g. activity_main.xml), add a TextView with the desired text and marquee attributes:

    ` <TextView

    android:id="@+id/marqueeTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:text="This is a marquee text view example."
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
    

    /> `

In this example, we set android:singleLine to true to ensure the text scrolls horizontally on a single line. The android:ellipsize attribute is set to "marquee" to enable the marquee effect. The android:marqueeRepeatLimit attribute is set to "marquee_forever" to make the marquee repeat indefinitely. The android:text attribute specifies the initial text to display.

Note: To make the marquee work, the TextView needs to have focus. Therefore, we set android:focusable and android:focusableInTouchMode to true to allow the TextView to gain focus.

  1. In your Kotlin code, you can optionally add the following lines to enable the marquee effect programmatically:

val marqueeTextView = findViewById<TextView>(R.id.marqueeTextView) marqueeTextView.isSelected = true

By setting isSelected to true, the TextView will start scrolling immediately when the view gains focus.

Make sure to replace R.id.marqueeTextView with the actual ID of your TextView in your code. Adjust the layout attributes and text content according to your desired appearance and behavior.