F
F
ftp272013-01-07 15:26:39
Android
ftp27, 2013-01-07 15:26:39

Marquee effect in TextView on Android?

I have a single line TextView with a fixed length. Unfortunately, for some of the texts that I put there, there is too little space. I decided to do something like a running line effect. Those. so that the text scrolls back and forth automatically. Most likely this is real, since I have already seen this somewhere in ready-made applications, but I did not find the necessary information on the Internet ...
a piece of Layout describing TextView

<TextView
              android:id="@+id/item_text"
              android:layout_width="fill_parent"
              android:layout_height="30dp"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:textColor="#fff"
              android:background="#8000"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:text="@string/item"/>

And a piece of code where attempts are made to “scroll” this text:
TextView text=(TextView)vi.findViewById(R.id.item_text);
text.setHorizontalFadingEdgeEnabled(true);
text.setHorizontallyScrolling(true);
text.setText(Item.title);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PVOID, 2013-01-07
@ftp27

Are you not satisfied with the built-in TextView capability?
android:ellipsize="marquee"
in xml or the same but in Java, look here already in the help.
The only thing that, as far as I remember, the text will run only if you tap on it

N
Ne-Lexa, 2013-11-21
@NeLexa

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

in xml:
<package.name.ScrollingTextView 
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:lines="1"
              android:scrollHorizontally="true"
              android:ellipsize="marquee"
              android:marqueeRepeatLimit="marquee_forever"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question