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.