In this article, we are going to create Marquee Text in Android Studio. Marquee is a scrolling piece of text that is displayed either horizontally or vertically. It is used to show some important notices or headlines. It makes the app UI much more attractive.
Note that we are going to use Java as the programming language.
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. TextView is used here to add the text which we want to display on the screen. Here we have used android:ellipsize=”marquee” to add a marquee to our text and android:singleLine=”true” so that our text will show only in one line. Also, we have used android:marqueeRepeatLimit=”marquee_forever” so that marquee will repeat infinitely and one more attribute that I have used here is android:scrollHorizontally=”true” so that text will scroll horizontally.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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"tools:context=".MainActivity"><!-- Textview is used here to add the text which we want to display on the screen.Here important attributes are: i) android:singleLine="true"... so that our text will show only in one line ii) android:ellipsize="marquee"... to add marquee to our text iii) android:marqueeRepeatLimit="marquee_forever"...so that marquee will repeat infinitely iv) android:scrollHorizontally="true"... so that text will scroll horizontally --><TextViewandroid:id="@+id/marqueeText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="25sp"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:padding="10dp"android:scrollHorizontally="true"android:singleLine="true"android:text="Hello guys !! Welcome to GeeksforGeeks Portal !!"android:textSize="20sp"android:textStyle="bold"/></RelativeLayout>
Step 3: Working with the MainActivity.java file
Go to MainActivity.java Class. We have called the setSelected() method and passing the boolean value as true so that our marquee will get started. Below is the code for the MainActivity.java file.
Java
importandroid.os.Bundle;importandroid.widget.TextView;importandroidx.appcompat.app.AppCompatActivity;publicclassMainActivityextendsAppCompatActivity{TextViewtxtMarquee;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// casting of textviewtxtMarquee=(TextView)findViewById(R.id.marqueeText);// Now we will call setSelected() method// and pass boolean value as truetxtMarquee.setSelected(true);}}
Step 4: Working with colors.xml file
Navigate to the app > res > values > colors.xml. You can add as many colors as you need for your app. You have to just give a color code and put the color name. In this app, we have kept the app bar color as green with the color-code “#0F9D58”.
Navigate to the app > res > values > themes.xml and choose the theme of your choice. We have used parent=”Theme.MaterialComponents.DayNight.DarkActionBar” that is DayNight theme with dark ActionBar. You can add parent=”Theme.AppCompat.Light.DarkActionBar” to get light theme with dark action bar and parent=”Theme.AppCompat.Light.DarkActionBar” for light theme with dark action bar.
XML
<resourcesxmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><stylename="Theme.MarqueeText"parent="Theme.MaterialComponents.DayNight.DarkActionBar"><!-- Primary brand color. --><itemname="colorPrimary">@color/Green</item><itemname="colorPrimaryVariant">@color/Green</item><itemname="colorOnPrimary">@color/white</item><!-- Secondary brand color. --><itemname="colorSecondary">@color/teal_200</item><itemname="colorSecondaryVariant">@color/teal_700</item><itemname="colorOnSecondary">@color/black</item><!-- Status bar color. --><itemname="android:statusBarColor"tools:targetApi="l">?attr/colorPrimaryVariant</item><!-- Customize your theme here. --></style></resources>
Step 6: Working with strings.xml
Navigate to the app > res > values > strings.xml. Here you can add an app bar title. We have set “GFG | MarqueeText” as a title.