How to Create Scrolling TextView in Android.

June 28, 2012 Zaidi Android Development TutorialsAndroid Layouts

On my last tutorial, I showed you how to create drawable background in Android using XML. Today I will shift gears to show you how to create an app with a scrolling TextView across the screen. Not exactly sure why you would do that but hey, there are so many ideas out there and scrolling text can be useful to someone who needs it.

This tutorial will be short and easy to understand even for beginners who are looking for easy way to learn Android app development. Before I show you how to implement, you need to have your specific Android project ready with its an XML file that you are going to add scrolling text.

Although Android TextView has built in marquee feature, this option alone is not enough to make displayed text scroll across the screen. Setting android:ellipsize=”marquee” is just a key step to avoid long text from being truncated. I will show you in a second what other attributes and their respective options that need to be declared in your XML layout file.

Now lets create a scrolling text that repeats 10 times. Open your XML layout file, decide which  TextView to scroll and set its attributes as shown below.

As you can see, XML layout does the heavy lifting. I have declared two extra attributes, android:singleLine=”true” and android:scrollHorizontally=”true”. ¬†The two attributes help position the TextView properly and make the text longer than the screen view. To make scrolling animation happen magically, set selected state to true in the main Java file.

If you want the text to scroll indefinitely, set android:marqueeRepeatLimit=”marquee_forever”. One important point to remember about marquee feature is, the text will only scroll when it is focused or selected. So if you run your project and nothing happens once the emulator is up and running, don’t panic.

That’s it for today and hope it will help you on whatever project or app idea you are trying to accomplish.

android ellipsize marqueeandroid marqueeRepeatLimitandroid scrollHorizontally trueandroid singleLine truescrolling animation textscrolling textview android


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Powered by WordPress and HQ Premium Themes.