Open In App

How to add TextSwitcher with animation in Android using Java

Last Updated : 11 Nov, 2022
Like Article

A TextSwitcher is used to animate a text on a screen. It is the child class of the ViewSwitcher class. It contains only one child of type TextView. In order to set animation on TextSwitcher we have to add animation tag or we can add programmatically. Here are the some usage of TextSwitcher:

  • Changing numbers in a Date Picker
    • Countdown of timer clock
    • TextSwitcher uses two type of animations:

      • In Animation
      • Out Animation


      1. Add the following code in the activity_main.xml file.


        <?xml version="1.0" encoding="utf-8"?>
                android:outAnimation="@android:anim/slide_out_right" />
                android:text="Next" />


      2. Now add the following code in the file.

        package com.madhav.maheshwari.gfgTextSwitcher;
        import android.os.Bundle;
        import android.view.Gravity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextSwitcher;
        import android.widget.TextView;
        import android.widget.ViewSwitcher;
        public class MainActivity extends AppCompatActivity {
            private TextSwitcher textSwitcher;
            private Button nextButton;
            private int index = 0;
            private String[] arr
                = { "GeeksForGeeks", "A",
                    "Computer", "Science",
                    "Portal", "For",
                    "Geeks" };
            private TextView textView;
            protected void onCreate(
                Bundle savedInstanceState)
                textSwitcher = findViewById(;
                nextButton = findViewById(;
                    new View.OnClickListener() {
                        public void onClick(View v)
                            // when the text switcher
                            // reaches at the end
                            // of our array. It will
                            // be reset to 0.
                            if (index == arr.length - 1) {
                                index = 0;
                            else {
                // Here we have to create
                // a TextView for our TextSwitcher
                    new ViewSwitcher.ViewFactory() {
                        public View makeView()
                                = new TextView(
                            return textView;
                // This is used to set the text
                // when app starts which is
                // at index i.e 0.



      Like Article
      Suggest improvement
      Share your thoughts in the comments

Similar Reads