Skip to content
Related Articles

Related Articles

How to add TextSwitcher with animation in Android using Java
  • Difficulty Level : Hard
  • Last Updated : 23 Feb, 2021

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 programtically. 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.


      Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

      My Personal Notes arrow_drop_up
Recommended Articles
Page :