How to create a scrolling text in a textview in android

In this tutorial we will learn how to create a scrolling text in a textview in android, It can be simply achieved using marquee . It can be achieved by adding few lines to the TextView in your layout and making it as setSelected in your class file to make textview focusable.

First step is to add the following lines to the TextView in your layout file.

android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"

So your textview will become

<TextView
android:id="@+id/tv_marquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/colorPrimary"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="This is a long text inside the textview to test scrolling of text in textview of android"
android:layout_centerInParent="true"/>

Now you need to make your textview selected in your class file

TextView tv_marquee = (TextView) findViewById(R.id.tv_marquee);
tv_marquee.setSelected(true);

I am using the following color in my colors.xml which is used in the above code.

<color name="colorPrimary">#3F51B5</color>

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *