Android TextSwitcher

TextSwitcher can be defined as below –

"Android TextSwitcher, subclass of ViewSwitcher, is a widget that contains only children of type TextView. Using animations, this is used to switch between views. It animates the current text out and animates the next text in."

Some of the popular attributes of android TextSwitcher inherited from viewAnimator are –



S. No. XML Attributes Descripitions
1 android:inAnimation Specifies the animation to use when view is shown.
2 android:outAnimation Specifies the animation to use when view is hidden.
3 android:animateFirstView Defines whether to animate the current view when the view animation is first displayed.

Some of the popular attributes of android TextSwitcher inherited from FrameLayout are –

S. No. XML Attributes Descripitions
1 android:measureAllChildren Specifies whether to measure all children or just those in VISIBLE or INVISIBLE state while measuring.
2 android:foregroundGravity Specifies gravity to apply to the foreground drawable.

Some of the popular attributes of android TextSwitcher inherited from ViewGroup are –

S. No. XML Attributes Descripitions
1 android:layoutAnimation Defines layout animation to use when the viewGroup is laid out for the first time.
2 android:animateLayoutChanges Defines whether layoutTransition should run when there is any changes in layout.
3 android:layoutMode Defines the layout mode of this viewGroup.
4 android:animationCache Defines whether layout animations should create drawing cache for their children.
5 android:clipToPadding efines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.

Some of the popular attributes of android TextSwitcher inherited from View are –



S. No. XML Attributes Descripitions
1 android:id This is unique id of the TextSwitcher to uniquely identify the TextSwitcher.
2 android:height Height of the TextSwitcher.
3 android:width Width of the TextSwitcher.
4 android:alpha Defines alpha of the view.
5 android:clickable Defines whether this view is clickable.
6 android:background Defines drawable for the background of the view.
7 android:elevation Defines z-depth of the view.
8 android:onClick Defines what to do when this view is clicked
9 android:padding Defines padding of the view
10 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view.


At first, we will create android application. Then, we will see how to build TextSwitcher in the application.

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project then choose your project and select Empty Activity.
3 Write application name as TextSwitcher after this write a package name you can write your own package name.
4 Select language Java. Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial  to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use TextSwitcher in the application.

Open res/values/strings.xml file. Then, add below code into it.

<string name="app_name">TextSwitcher</string>
<string name="next">Next</string>



Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android=""


 android:layout_gravity="center" />

 android:text="@string/next" />


In activity_main.xml file, We've described buttons and textSwitcher widgets. We'll now access these widgets in the java file to conduct some activities on them.


Open src/main/java/com.ukacademe.textswitcher/ file. Then, add below code into it.

package com.ukacademe.textswitcher;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

    final String[] textList = {"C", "C++", "Java", "Xml", "HTML", "C#", "Php", "", "JavaScript"};
    int index = 0;

    protected void onCreate(Bundle savedInstanceState) {

        final TextSwitcher textSwitcher = findViewById(;
        if (textSwitcher != null) {
            textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
                public View makeView() {
                    TextView textView = new TextView(MainActivity.this);
                    textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                    return textView;

            Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

            Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

            Button button = findViewById(;
            button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    index = ((index + 1) < textList.length) ? (index + 1) : 0;



In the java file, we have accessed the TextSwitcher. Then we set a plant that defines the perspective between which TextSwitcher will switch. Then we put in and out animations that will be used when TextView appears/disappears on/from the screen.

Finally, we have accessed the button and set the click listener to display the next textViews when clicked.

Since AndroidManifest.xml file is very important in any android application, we'll also see the content in this file.

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android=""

 <activity android:name=".MainActivity">
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />


When we run the program, we'll get output as shown below.