Hello Friends, In this tutorial I will be showing how we can create Marquee text in Android.
Step 1:- Creating New Project
Create a new project in Android Studio from File ⇒ New Project Fill the required information like project name and package name of the project. When it prompts you to select the default activity, select Empty Activity and proceed.
Step 2:- Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/mScrollingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:fadingEdge = "horizontal"
android:text="Create a Marquee (Scrolling Text) in Androidx Using TextView. Androidx Marquee (Scrolling Text) Tutorial with Example"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp" />
</RelativeLayout>
In the above code, we have taken text view and ellipsize property as marquee as shown below :-
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:singleLine = "true"
Step 3:- Add the following code to src/MainActivity.java
package in.imranalam.app.marqueetext;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = findViewById(R.id.mScrollingText);
mText.setSelected(true);
}
}
Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run.
icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −