Skip to content Skip to sidebar Skip to footer

Android Textview Marquee Not Working.

How come the following code not working. I have something similar at other part of the layout and it works. But this part does not work. Any idea?

Solution 1:

First, the text has to be longer than the "box" in order to marquee, if it fits-no marquee. I use this code:

<TextView
        android:id="@+id/availability"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="#343434"
        android:textSize="@dimen/text_size_medium"
        android:textStyle="bold|italic" />

Setting android:focusable="false" and android:focusableInTouchMode="false" allows it to marquee without focus.

Solution 2:

First, you can try this:

textview.setSelected(true); 

But it might not work when the text view can't get the focus all the time. So, to ensure TextView Marquee working, I had to use a custom TextView.

publicclassCustomTextViewextendsTextView {
    publicCustomTextView(Context context) {
        super(context);
    }
    publicCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    publicCustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


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

    @OverridepublicvoidonWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }


    @OverridepublicbooleanisFocused() {
        returntrue;
    }
}

And then, you can use the custom TextView as the scrolling text view in your layout .xml file like this:

<com.example.myapplication.CustomTextView
            android:id="@+id/tvScrollingMessage"
            android:text="@string/scrolling_message_main_wish_list"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/black"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:freezesText="true"/>

NOTE: in the above code snippet com.example.myapplication is an example package name and should be replaced by your own package name.

Hope this will help you. Cheers!

Post a Comment for "Android Textview Marquee Not Working."