Open In App

How to Implement TextSwitcher in Android?

Last Updated : 17 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Android, TextSwitcher is a useful tool for displaying text with animations. It can be seen as a special version of ViewSwitcher, where its children are only TextView elements. TextSwitcher allows us to add smooth transitions to text, making it part of the transition widget family. Whenever you call the setText(CharSequence) method, the TextSwitcher animates the current text out and the new text in. This can be done with a single button or multiple buttons. For a single button, we cycle through the text in an array.

What are we going to build in this article?

In this article, we will create a simple app that implements TextSwitcher. When you click the “Next” button, the text will change in a smooth, serial manner.

TextSwitcher is available in Android from version Android 1.6+.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with activity_main.xml

In this file, we use the TextSwitcher, and Buttons and also set their attributes.

activity_main.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray_btn_bg_color"
        android:layout_gravity="center_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4" />


    <Button
        android:id="@+id/prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:backgroundTint="@color/main_green_stroke_color"
        android:text="Prev"
        app:layout_constraintEnd_toStartOf="@+id/next"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textSwitcher" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:backgroundTint="@color/main_green_stroke_color"
        android:text="Next"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/prev"
        app:layout_constraintTop_toBottomOf="@+id/textSwitcher" />

</androidx.constraintlayout.widget.ConstraintLayout>

Design UI:

design-ui-textswitcher

Step 3: Access TextSwitcher in MainActivity file

First, we declare an array which contains the list of numbers used for the textView.

private val array = arrayOf("1","2","3","4","5")

then, we access the TextSwitcher from the XML layout and set attributes like text color, text Size.

val textSwitcher: TextSwitcher = findViewById(R.id.textSwitcher)
MainActivity.java
package org.geeksforgeeks.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private final String[] array = {"1", "2", "3", "4", "5"};
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Accessing the TextSwitcher from XML layout
        TextSwitcher textSwitcher = findViewById(R.id.textSwitcher);
        textSwitcher.setFactory(() -> {
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            textView.setTextSize(32f);
            textView.setTextColor(Color.RED);
            return textView;
        });

        // Set the initial text
        textSwitcher.setText(array[index]);

        // Load in and out animations for TextSwitcher
        textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
        textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));

        // Previous button functionality
        Button prev = findViewById(R.id.prev);
        prev.setOnClickListener(v -> {
            index = (index - 1 >= 0) ? index - 1 : 4;
            textSwitcher.setText(array[index]);
        });

        // Next button functionality
        Button next = findViewById(R.id.next);
        next.setOnClickListener(v -> {
            index = (index + 1 < array.length) ? index + 1 : 0;
            textSwitcher.setText(array[index]);
        });
    }
}
MainActivity.kt

Output:

Click the next button then we obtain the other text in the TextView.




Next Article

Similar Reads

three90RightbarBannerImg