5

I want to create a scrolling text ticker for a simple android app.

I have a large list of quotes stored in an array, and I would like to randomly populate my ticker with the quotes: one quote scrolls all the way through, then another is randomly chosen and scrolls its way through, and so on.

The ticker should chug along regardless of what's been focused on or hovered over...

1) How do I create a text ticker for these purposes?

2) How do I populate the ticker with a steady stream of random quotes selected from my array?

Thanks

Rob
  • 762
  • 2
  • 21
  • 44

1 Answers1

7

If I'm understand what you're trying to do properly you want to look at the ellipsize and marqueeRepeatLimit properties of TextView.

Via the API:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

http://developer.android.com/reference/android/widget/TextView.html#attr_android:marqueeRepeatLimit

Also, look at this question for an implementation. From what I remember when I had to implement something like this is that the XML properties can be tricky. They interfere with one another and prevent the text from scrolling across the screen so it may take some toying with to get it right.

From there, populating it with different quotes is as simple as calling setText() on the TextView with the random quote that I assume you'll have stored in an array or database at the proper time.

Community
  • 1
  • 1
shanet
  • 7,246
  • 3
  • 34
  • 46
  • how will i know to call setText()? I would like to set the text as soon as the current string has run all the way through, but how would I know when that's happened? – Rob Oct 12 '11 at 18:57
  • How many quotes are there? If it's not too many, create a string beforehand. Randomly pull all the quotes from your array and compile them into one large string and set that as the text in the TextView. – shanet Oct 12 '11 at 20:21
  • well over 1000 quotes, it won't be practical to use a single string. – Rob Oct 13 '11 at 00:40
  • You'll need to get creative then. One possible, albeit messy, solution may be to get the width of the TextView, the length of the string (you'll need to use a monospace font), and the scrolling speed of the TextView (it's constant but you'll need to experimentally determine the speed). Use that to calculate the time. Then use either timers or sleep a separate thread for the calculated time, then change the string. I'm not sure if that would even work, but it might be a good place to start. There might be something in the API as well that would circumvent all of this, but I'm ignorant to it. – shanet Oct 13 '11 at 04:41
  • See http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#width for the width of a View. If the View is `MATCH_PARENT` or `WRAP_CONTENT` you'll need to get the parent View and check it's width. This is, unfortunately, getting very tedious very fast. There must be a better solution. – shanet Oct 13 '11 at 04:41
  • I'm going to abandon the ticker and replace it with static quotes that replace one another every 5 seconds. – Rob Oct 13 '11 at 14:36
  • That works too. It might be better from a usability standpoint as well. Tickers look nice, but not everyone reads at the same speed. – shanet Oct 13 '11 at 20:33