How to Implement TextSwitcher in Android?
Last Updated :
17 Feb, 2025
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:
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)
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
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.
Similar Reads
How to Implement TextWatcher in Android?
If an application contains a login form to be filled by the user, the login button should be disabled (meaning: it shouldn't be clickable). When the user enters the credentials of the form the button should be enabled to click for the user. So in this article, we are implementing a Text Watcher to t
3 min read
How to Implement PDF Picker in Android?
In this article, we are going to see how we can implement a PDF picker in android studio and get the file information of the pdf like file name, size and path. In this application, we will create a launcher to launch the file picker and display the file information after a pdf is picked successfully
5 min read
How to implement View Shaker in Android
View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials. View Shaker helps us to animate the widgets.
3 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement OTP View in Android?
An OTP View or PinView in android is a widget that allows users to enter their PIN, OTP, etc. They are generally used for two-factor authentication or phone number verification by OTP. A sample video is given below to get an idea about what we are going to do in this article. Note: In order to imple
2 min read
How to Implement Tooltip in Android Studio?
A tooltip is a message which appears when a cursor is positioned over an icon, image, hyperlink, or any other GUI component. In this application, we will be using an EditText to take the message from the user and then display that message as a tooltip over a view. Here is a sample video of the appli
4 min read
How to Implement Item Click Interface in Android?
When we click on an item in an application either it gives some information or it redirects the user to any other page. In this article, we will learn that how we can implement Item Click Interface in an android application. What we are going to build in this article?In this article, we will be usin
5 min read
How to Implement TNImage Library in Android?
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
3 min read
How to Implement OnSavedInstanceState in Android?
In android, Preserving and restoring an activity's UI state in a timely fashion across system-initiated activity or application destruction is a crucial part of the user experience. In these cases the user expects the UI state to remain the same, but the system destroys the activity and any state st
4 min read
How to Implement Country Code Picker in Android?
Country Code Picker (CCP) is an android library that helps users to select country codes (country phone codes) for telephonic forms. CCP provided a UI component that helps the user to select country codes, country flags, and many more in an android spinner. It gives well-designed looks to forms on t
3 min read