Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic horizontal scroll in TextView

I have custom gallery. Gallery represents items that are frame layout. There are one imageView and textView above it.

If text in textView is too long, i need it to be scrolled automatically. It's one line of text, and it's needed to be scrolled horizontally.

I've found this snippet of code:

TextView
    android:text="Single-line text view that scrolls automatically"       
    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>   

It works in my test app with only one text view in it. But it doesn't work in my gallery. Noting happens, text just stay still.

Any help?

like image 621
Veljko Avatar asked Feb 23 '12 20:02

Veljko


2 Answers

Try this custom TextView class:

public class AutoScrollingTextView extends TextView {
    public AutoScrollingTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public AutoScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoScrollingTextView(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;
    }
}

and set the following XML attributes:

android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"

This works beautifully in my dictionary apps where multiple entries may need to auto-scroll simultaneously to display complete content.

like image 53
Kai Avatar answered Nov 09 '22 04:11

Kai


The marquee effect on a TextView is only designed to work when the view is focused or selected. The XML code you have tries to make the TextView focused all the time. Unfortunately, since only one view can be focused at any time, and since you have multiple views in the gallery, this approach will not work for you.

The easiest way to accomplish this otherwise is to make the TextViews always be selected. Multiple TextViews can hold the selected state at one time. Selection is meant to be used for an active element of an AdapterView, but still works outside of one. Firstly, remove the attributes modifying the focus from the XML and then just call TextView.setSelected(true) sometime after the view is initialised, e.g. in Activity.onCreate(Bundle) (there is no XML attribute for this). If you are supplying the views from an adapter, then you can call TextView.setSelected(true) during the getView() method after you inflate the view.

Here is an example project showing marquee working for multiple TextViews, and the behaviour inside a Gallery.

like image 25
antonyt Avatar answered Nov 09 '22 03:11

antonyt