Showing posts with label auto-Scrolling Marquee TextView. Show all posts
Showing posts with label auto-Scrolling Marquee TextView. Show all posts

Wednesday, December 14, 2011

Create an auto-Scrolling Marquee TextView in android

AIM:
Create a  textview that scroll text if text is not fit in the specified width,without need to focus just like Android Market app description screen.

Solution:
This solution able to scroll text inside a TextView without it required to be focused. For some strange reason there isn't an easy way to do this natively.

Generally, textview will marque the text when it is focused only.
By using the below code,the textview will automatically scroll even without focus to textview.

Step 1: Create an autoscrolltextview by extending textview (ScrollingTextView.java)
public class ScrollingTextView extends TextView {

    public ScrollingTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollingTextView(Context context) {
        super(context);
    }

    @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;
    }
}

Step 2:  Use this XML tag in your XML layout.
   <com.test.autoscroll.ScrollingTextView
                android:id="@+id/actionbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:textSize="16dip"
                android:textStyle="bold"
                android:lines="1"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                 android:text="autoscrollable textview without focus to textview...working...."
                android:marqueeRepeatLimit="marquee_forever"
                />
This will get you the scrolling marquee behavior desired out of the TextView control!
 

Android Developers Blog

Ram's shared items