Search code examples
javaandroidxmlandroid-5.0-lollipopmarquee

Textview Marquee not scrolling in lollipop version


I have marquee for text view and its scrolling in pre-lollipop versions smoothly. But cannot work in lollipop versions and above . Please help me on this.

Following are code for XML.

   <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView1"
    android:layout_gravity="center_horizontal"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:layout_marginTop="-12dp"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textColor="@color/white"
    android:background="#00AEEF"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fadingEdge="horizontal"
    android:freezesText="true"
   />

Solution

  • create a MyTextView extends TextView then override isFocus()

    public class MyTextView extends TextView {
    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    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() {
        //always focus  
        return true;
    }
    }
    

    then use

      <common.widget.MyTextView
        android:id="@+id/tv_autoplay"
        android:background="#CDC9C4"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#fff"
        android:gravity="center"
        android:text="marqueemarqueemarqueemarqueemarqueemarquee……………………"
        android:layout_width="match_parent"
        android:layout_height="30dp" />