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();