Friday, July 30, 2010

Android Title Marquee

I recently wanted the title of my activity to scroll like a marquee whenever it was too long to fit in its space (because it is fetched from the net and can be pretty much anything). I didn't want to use a custom multi-line title view because that would waste screen space and because I want to preserve the native look and feel. The solution I've found is rather hackish, but it works. Here goes the code, with explanatory comments and all:

// make the title scroll! // find the title TextView TextView title = (TextView) findViewById(android.R.id.title); // set the ellipsize mode to MARQUEE and make it scroll only once title.setEllipsize(TruncateAt.MARQUEE); title.setMarqueeRepeatLimit(1); // in order to start strolling, it has to be focusable and focused title.setFocusable(true); title.setFocusableInTouchMode(true); title.requestFocus();

7 comments:

  1. Hi. I tried the code but does not work. Even I set max lines to 1. Are you sure TextView is focusable?

    ReplyDelete
  2. Yew Felix I too didnt find this working..

    ReplyDelete
  3. Thanks, worked straight away as-is. The only edit I needed was to change to setMarqueeRepeatLimit(-1) for continuous scrolling.

    ReplyDelete
  4. Thanks, worked out of the box.

    ReplyDelete
  5. I'm not sure why you think that's "hackish". That's simply a feature of the system. :D

    ReplyDelete
  6. It doesn't work for me either...

    ReplyDelete
  7. Make sure of
    1)title.setMarqueeRepeatLimit(-1); in java file
    2)android:singleLine="true" attribute in TextView of layout.xml file.
    3) in manifest file.

    Works like a charm...Thanks Felix :)

    ReplyDelete