Android TextSwitcher Using Kotlin With Example

Hello Readers! In this post, we are going to learn about android textSwitcher using kotlin in any android application. We will also learn about different attributes of android textSwitcher that can be used to customise this widget.


Tutorialwing Kotlin TextSwitcher Output Android TextSwitcher Using Kotlin With Example

Tutorialwing Kotlin TextSwitcher Output

Getting Started

Android TextSwitcher can be defined as below –

TextSwitcher, a subclass of ViewSwitcher, is a widget that contains children of type textView. It is used to animates current text out and next text in.

Different Attributes of Android TextSwitcher Widget

Some of the popular attributes of android textSwitcher widget inherited from viewAnimator are –

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

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

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

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

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

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

Sr. XML Attributes Description
1 android:alpha Specifies alpha of the view
2 android:clickable Specifies whether this view is clickable
3 android:background Specifies drawable for the background of the view
4 android:elevation Specifies z-depth of the view
5 android:id Specifies unique id of the view
6 android:onClick Specifies what to do when this view is clicked
7 android:padding Specifies padding of the view
8 android:visibility Specifies visibility(VISIBLE, INVISIBLE or GONE) of the view

Example of Android TextSwitcher Using Kotlin

At first, we will create android application. Then, we will use textSwitcher using kotlin in the application.

1. Creating New Project in Kotlin

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TextSwitcher. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use textSwitcher using kotlin in the application.

2. Modify values folder

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

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

3. Use TextSwitcher Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>




In activity_main.xml file, we have defined button and textSwitcher widgets. Now, we will access these widgets in java file to perform some operations on them.

4. Access TextSwitcher Widget in Kotlin file

Open src/main/java/com.tutorialwing.textswitcher/MainActivity.kt file and add below code into it.

package com.tutorialwing.textswitcher

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

class MainActivity : AppCompatActivity() {

    private val textList = arrayOf("Panda", "Tiger", "Zebra", "Lion", "Deer", "Goat", "Ape", "Monkey", "Human")
    private var index = 0

    override fun onCreate(savedInstanceState: Bundle?) {

        val textSwitcher = findViewById<TextSwitcher>(
        textSwitcher.setFactory {
            val textView = TextView(this@MainActivity)
            textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
            textView.textSize = 32f

        val `in` = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
        textSwitcher.inAnimation = `in`

        val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
        textSwitcher.outAnimation = out

        val button = findViewById<Button>(
        button.setOnClickListener {
            index = if (index + 1 < textList.size) index + 1 else 0

In file, we have accessed textSwitcher widget. Then, we have set factory to define the views which will be used by textSwitcher. Then, we have set in and out animations that are used when current text is animated out and next text is animated in. Then, we have set click listener on button to show next text in textSwitcher.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.textswitcher"

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

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


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android TextSwitcher using Kotlin.