How To Create Scrolling Text In Android

Scrolling-Text-In-Android-thumb

24th February, 2020

Hi Android developer!

You may have seen scrolling text somewhere in websites or apps.

See this HTML code.

Input:

<marquee>This is basic example of marquee text in html</marquee>

Ouput:

This is basic example of marquee text in html

1. Scrolling TextView in Layout xml

Let’s see how to create a scrolling text in Android? Just add some additional properties to the TextView, which enables it to scroll.

<TextView
...
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
.../>

Copy the following TextView and paste it in your layout file wherever you want to display scrolling text.

<TextView
        android:text="This is marquee textview example... Android is an operating system for mobile devices || Android is the world's most popular operating system for mobile devices and tablets || It is an open source operating system"
        android:id="@+id/MarqueeText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:focusableInTouchMode="true"
        android:freezesText="true"
        android:textColor="#000000"/>

2. Scrolling TextView Programmatically

If you want to set TextView’s properties programmatically. Let’s see how to do this.

TextView myTextView = findViewById(R.id.MarqueeText);
        myTextView.setSingleLine(true);
        myTextView.setMarqueeRepeatLimit(-1);
        myTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        myTextView.setSelected(true);

Output: Video Demo

Happy coding!

Leave a Reply