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!
 

4 comments:

  1. Thanks for the solution - I couldn't use the simpler setSelected(true) solution, as I have an AsyncTask that continuously updates the view and was stealing the focus. And you're right, there should be an easier native way to do this...

    ReplyDelete
  2. Yu can have that in either onPostExecure which will be in main thread.( or on progress change )

    ReplyDelete
  3. As imposing as clock speeds are, they likely can't measure up to the" pleasant" smell of lemon. You will find it easy to track when based upon the sales volume of their group.

    Feel free to visit my homepage; Student Loan

    ReplyDelete
  4. I want to scroll the text automatically on app start and it should scroll vertically not horozontally. so, please can u give me the solution.
    Thank You.

    ReplyDelete

Android Developers Blog

Ram's shared items