Wednesday, 5 June 2013

Marquee effect in TextView tutorial

By this post i am gonna teach about How to set Marquee effect in Android Text View.

Design Code (XML)

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>
Activity Code. 
public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.tv);  
        tv.setSelected(true);  // Set focus to the textview

// in case if you want to do it this in problematically means please use the below lines.
        textview.setText("your long text");
       
textview.setSelected(true);
       
textview.setEllipsize(TruncateAt.MARQUEE);
       
textview.setSingleLine(true);
    }
}
 Hope this helps . Happy Coding.

7 comments:

Unknown said...

It doenst work if I change the text on the TextView time from time.

Only the first text set works.

Unknown said...

It not works for short text..how can i set marquee for short text.

Nick said...
This comment has been removed by the author.
Nick said...

@Namrata and @Rafael
Try This One.




and After that in your mainactivity
Textview marqee = (Textview)findviewbyId(R.id.MarqueeText);
marqee.setSelected(true)
dont forget to do this line..!

Unknown said...

it is not working for short text. why?

Unknown said...

When i take textview property as maxLines = "1" . Then this marquee is not running and why. Please give me suggestions ...

Unknown said...

Animation animationToRight = new TranslateAnimation(-400,400, 0, 0);
animationToRight.setDuration(16000);
animationToRight.setRepeatMode(Animation.RESTART);
animationToRight.setRepeatCount(Animation.INFINITE);
Animation animationToLeft = new TranslateAnimation(500, -400, 0, 0);
animationToLeft.setDuration(16000);
animationToLeft.setRepeatMode(Animation.RESTART);
animationToLeft.setRepeatCount(Animation.INFINITE);

TextView tv = (TextView)findViewById(R.id.txt_name);
//tv.setAnimation(animationToRight);
tv.setAnimation(animationToLeft);
tv.setSelected(true);
String textRight = "Find your favourite product and buy it";
tv.setText(textRight);