I want the text in TextView to flow from left to right, what should I do?

Asked 2 years ago, Updated 2 years ago, 132 views

I'm making an app, and I want to keep the text view flowing at the top of the app as an announcement, so I don't know what to do I don't know. Masters, please teach us.

android textview

2022-09-22 12:39

1 Answers

<TextView
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"/>

You can attribute it this way, but it only flows if the focus is located in TextView. Because of that, Create a custom text view

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class MarqueeTextView extends TextView{
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

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

In the layout xml, you must replace the TextView with a custom text view.

<com.test.MarqueeTextView
android:id="@+id/textViewTitleOnDrawBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent""
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Flowing text~~~~~~~"/>


2022-09-22 12:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.