3

I'm currently playing with the marquee feature of Android's Textview. But there is a problem- I have a layout containing a few TextView. On one of them, which shows a title of an music file, I want to apply the marquee feature. On another, I keep updating the current track time (every second).

The problem is, everytime the TextView which shows the track time is updated (setText()), the marquee of the other TextView stops. I guess this is because setTExt() moves the focus to the TextView which I to apply the text to.

Is there a solution for this?

I used a custom class for the marquee TextView from this tutorial

Is there a possibility (maybe a custom class as well) for the other TextView to not "steal" focus?

Thanks!

Regards

6 Answers 6

4

Marquee is enabled when a TextView is setEnabled(true) or receives focus. Use setEnabled(true) to make sure other TextViews won't stop their marquee when focus moves.

1
4

In my case setEnabled(true) hasn't hepled. Neither requesting focus, nor anything of the sort, because it hasn't been a problem with focus. I believe marquee also restarts whenever the TextView goes through onLayout().

In my case the TextView was inside a LinearLayout that had layout_height="wrap_content". The TextView showing the track time was below the TextView with marquee. Now when each second I was updating the lower TextView, then I believe the OS computed it's height from scratch and also computed the height of the whole LinearLayout based on the height of TextView (which, of course, was the same every time). A that's why the whole LinearLayout (and it's children) was getting onLayout() which restarted the marquee.

Long story short, I set the height of my container LinearLayout to a fixed value and marquee started to work.

2
  • This worked for me. setEnabled(boolean b) had no effect. Thanks for the answer!
    – b.lyte
    Feb 5, 2015 at 20:00
  • 1
    Just want to add that you could also change the height or width attribute of the textview on which setText is called. In my case I couldnt change the layout attribute to anything else than wrap_content because of layout concerns.
    – Max Power
    Nov 30, 2019 at 11:18
3

But there is a problem- I have a layout containing a few TextView. I found the solution here: http://androidbears.stellarpc.net/?p=185

You should extend TextView class and override several methods

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    if(focused)
       super.onFocusChanged(focused, direction, previouslyFocusedRect);
}



@Override
public void onWindowFocusChanged(boolean focused) {
    if(focused)
       super.onWindowFocusChanged(focused);
}

@Override
public boolean isFocused() {
       return true;
}
1

You can simply requestFocus(); back onto the TextView you want to marquee after the new music TextView has updated. If you can't accomplish this, post some code an we can help.

TextView.requestFocus()

2
  • Hi again. Thanks for the quick answer. I think I did not explain myself right, sorry. Because I update the TextView EVERY SECOND, the marquee doesn't even begin because this update cycle is just too fast. This is why I ask myself if a TextView can be updated without gaining focus, so that the focas will stay at the TextView which uses the marquee.
    – Georg
    Feb 11, 2011 at 23:55
  • What about what Romain recommends and set the textview you want to marquee to setEnabled(true); ?
    – Blundell
    Feb 12, 2011 at 18:35
1

See my answer here : https://stackoverflow.com/a/13841982/1823503

The idea is :

  • to fix the width and height programmatically
  • to setSelected
  • to Override your TextView to fix the focus problems
0

You can simply wrap the marquee TextView with a LinearLayout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.