How to Implement TextSwitcher in Android?
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:

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)
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]);
});
}
}
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
class MainActivity : AppCompatActivity() {
private val array = arrayOf("1","2","3","4","5")
private var index = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// accessing the TextSwitcher from XML layout
val textSwitcher = findViewById<TextSwitcher>(R.id.textSwitcher)
textSwitcher.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 32f
textView.setTextColor(Color.RED)
textView
}
textSwitcher.setText(array[index])
val textIn = AnimationUtils.loadAnimation(
this, android.R.anim.slide_in_left)
textSwitcher.inAnimation = textIn
val textOut = AnimationUtils.loadAnimation(
this, android.R.anim.slide_out_right)
textSwitcher.outAnimation = textOut
// previous button functionality
val prev = findViewById<Button>(R.id.prev)
prev.setOnClickListener {
index = if (index - 1 >= 0) index - 1 else 4
textSwitcher.setText(array[index])
}
// next button functionality
val next = findViewById<Button>(R.id.next)
next.setOnClickListener {
index = if (index + 1 < array.size) index + 1 else 0
textSwitcher.setText(array[index])
}
}
}
Output:
Click the next button then we obtain the other text in the TextView.