How to create a Scrolling Text (marquee) in android using TextView?

By | September 15, 2012

OK At first we will create the XML that contains a TextView.

marqueetext.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <TextView
    android:text="Really Long Scrolling Text Goes Here.... ..... ............ .... ...."
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:id="@+id/TextView03"
    android:padding="5dip" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />
   
</RelativeLayout>

Now the java code

package com.example.marqueetext;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class Marqueetext extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marqueetext);
        
        TextView tv = (TextView) this.findViewById(R.id.TextView03);  
        tv.setSelected(true);  // Set focus to the textview
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.marqueetext, menu);
        return true;
    }
}

Note : The XMl alone will not create the marquee. For that the most important thing is to do this in java code.

 tv.setSelected(true);  // Set focus to the textview

Now run the project and see the result.

Marquee

Marquee

9 thoughts on “How to create a Scrolling Text (marquee) in android using TextView?

  1. sai

    getMenuInflater().inflate(R.menu.marqueetext, menu);
    In the above line i’m getting an error and that’s it…….
    regarding that the code was very good and easily understandable…..

    Reply
    1. James Post author

      what is the error you are getting? Is it crashing the application?

      Reply
  2. vinod

    The code was easily understandable but i want to add more than one options in single marquee….can u plss suggest the code

    Reply
  3. AKHTAR ALI SIDDIQUI

    its really good site for understanding the android code

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *