Filtri Menu 0 0.00

Avoid scrolling reset (marquee ellipsize) on Android TextView

Avoid scrolling reset (marquee ellipsize) on Android TextView

This post taken from stack overflow is very very useful to save a lot of time and coding!

http://stackoverflow.com/questions/28305745/textview-scrolling-is-resetting-when-i-update-a-different-textview-from-the-same

The problem is that the horizontal scroll animation on an Android TextView in mode ellipsize marquee is reset every time in the containing layout something changes when a relative size is set on the TextView (MATCH_PARENT and/or WRAP_CONTENT).

If we specify fixed size the reset does not happens, how to do?

TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
tv.setSingleLine(true);
tv.setHorizontallyScrolling(true);
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.setMarqueeRepeatLimit(-1);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setSelected(true);

//the most important!!!!!
tv.post(new Runnable() {
    @Override
    public void run() {
        tv.setLayoutParams(new LinearLayout.LayoutParams(tv.getWidth(), tv.getHeight()));
    }
});
Share:

Subscribe to newsletter

Subscribe to our newsletter to receive early discount offers, updates and new products info.
Top