Open In App
Related Articles

TextSwitcher in Kotlin

Like Article
Save Article
Report issue

Android TextSwitcher is a user interface widget that contains a number of textView and displays one at a time. Textswitcher is a subclass of View Switcher which is used to animate one text and display the next one. Generally, we use TextSwitcher in two ways manually in XML layout and programmatically in Kotlin file. We should define an XML component, to use TextSwitcher in our android application. 


<TextSwitcher android:id="@+id/imgSw"


First, we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click the next button.
  4. Then select the Empty activity => next => finish.

Different attributes of the TextSwitcher widget

XML attributesDescription
android:idUsed to specify the id of the view.
android:onClickUsed to specify the action when this view is clicked.
android:backgroundUsed to set the background of the view.
android:paddingUsed to set the padding of the view.
android:visibilityUsed to set the visibility of the view.
android:inAnimationUsed to define the animation to use when the view is shown.
android:outAnimationUsed to define the animation to use when the view is hidden.
android:animateFirstViewUsed to define whether to animate the current view when the view animation is first displayed.

Modify activity_main.xml file

In this file, we use the TextSwitcher, and Buttons and also set their attributes. 


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintTop_toBottomOf="@+id/textSwitcher" />
        app:layout_constraintTop_toTopOf="@+id/prev" />


Update strings.xml file

Here, we update the name of the application using the string tag. 


    <string name="app_name">TextSwitcherInKotlin</string>
    <string name="next">Next</string>
    <string name="prev">Prev</string>


Access TextSwitcher in MainActivity.kt file

First, we declare an array languages which contains the list of languages used for the textView.

private val textList = arrayOf("Java","Python","Kotlin","Scala","C++")

then, we access the TextSwitcher from the XML layout and set attributes like text color, text Size.

val textSwitcher = findViewById<TextSwitcher>(


package com.geeksforgeeks.myfirstkotlinapp
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 languages = arrayOf("Java","Python","Kotlin","Scala","C++")
    private var index = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        // accessing the TextSwitcher from XML layout
        val textSwitcher = findViewById<TextSwitcher>(
        textSwitcher.setFactory {
            val textView = TextView(this@MainActivity)
            textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
            textView.textSize = 32f
        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>(
        prev.setOnClickListener {
            index = if (index - 1 >= 0) index - 1 else 4
        // next button functionality
        val button = findViewById<Button>(
        button.setOnClickListener {
            index = if (index + 1 < languages.size) index + 1 else 0


AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />


Run as Emulator:

Click the next button then we obtain the other text in the TextView.

Last Updated : 02 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads