views:

1681

answers:

5

I want to use the marquee effect on a TextView, but the text is only being scrolled when the TextView gets focus. That's a problem, because in my case, it can't.

I am using:

  android:ellipsize="marquee"
  android:marqueeRepeatLimit="marquee_forever"

Is there a way to have the TextView always scroll its text? I've seen this being done in the Android Market app, where the app name will scroll in the title bar, even if it doesn't receive focus, but I couldn't find this being mentioned in the API docs.

A: 

Try to set the focus of the TextView manually

Chris
Like I said in the question, the TextView is not focusable.
Matthias
+7  A: 

I have been facing the problem and the shortest solution I have come up with is to create a new class derived from TextView. The class should override three methods onFocusChanged, onWindowFocusChanged and isFocused to make the TextView all focused.

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

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


@Override
public boolean isFocused() {
    return true;
}
interesting, I'll try that, thanks!
Matthias
Perfect - this is just the solution I was looking for.
Jason
press menu button, the marquee will stop..
virsir
You should also override onWindowFocusChanged method if you want the marquee would not stop when menus pop up.
virsir
My hero!!!!!!!!!
Cristian
+3  A: 

I don't know if you still need the answer, but I found an easy way to do this.

Set up your animation like so:

Animation mAnimation = new TranslateAnimation(START_POS_X, END_POS_X, 
                START_POS_Y, END_POS_Y);
mAnimation.setDuration(TICKER_DURATION); 
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);

START_POS_X, END_POS_X, START_POS_Y and END_POS_Y are float values, while TICKER_DURATION is an int I declared with my other constants.

Then you can now apply this animation to your TextView:

TextView tickerText = (TextView) findViewById(R.id.ticker);
tickerText.setAnimation(mAnimation);

And that's it. :)

My animation starts on the right side off-screen (300f) and ends on the left side off-screen(-300f), with a duration of 15s (15000).

Zarah
That doesn't seem very simple. But I don't think he still needs the answer; see the accepted answer above.. :)
Christopher
interesting -- will try, too, thanks
Matthias
But I think this solution is simpler than creating a new class. :D As it is, you can also re-use the animation object for other views you want to animate. ;)
Zarah
A: 
    Just put these parameters in your TextView. It works :)

    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:scrollHorizontally="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 
Vinayak
+2  A: 
Christopher
just wondering: would this also work for TextViews that are focusable, but should still scroll when not being focused? I.o.w., does the select state "stick" even when the focus state changes?
Matthias
I guess so. I don't see why a focus change on a basic `TextView` (i.e. one not embedded in something "selectable" like a `ListView`) would change the selected state.
Christopher