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.
<?xml version="1.0" encoding="utf-8"?>
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.
TextView tView = (TextView) findViewById(R.id.myText);
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.