Open In App

Dynamic TextSwitcher in Kotlin

Last Updated : 15 Mar, 2022
Like Article

Android TextSwitcher is a user interface widget that contains number of textView and displays one at a time. Textswitcher is subclass of View Switcher which is used to animates one text and displays next one.

Here, we create TextSwitcher programmatically in Kotlin file.

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 next button.
  4. Then select the Empty activity => next => finish.

Modify activity_main.xml file

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


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />
        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">DynamicTextSwitcherInKotlin</string>
    <string name="next">Next</string>
    <string name="prev">Prev</string>

Create 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 create the TextSwitcher and set attributes for textView like text color, text Size.

val textSwitcher = findViewById<TextSwitcher>(

Add the TextSwitcher in layout using this

val c_Layout = findViewById(
        //add textSwitcher in constraint layout


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
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
    private val languages = arrayOf("Java","Python","Kotlin","Scala","C++")
    private var index = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        val textSwitcher = TextSwitcher(this)
        textSwitcher.setFactory {
            val textView = TextView(this@MainActivity)
            textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
            textView.textSize = 32f
            textView.setPadding(175, 50, 100, 200)
        val c_Layout = findViewById<ConstraintLayout>(
        //add textSwitcher in constraint layout
        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 next = findViewById<Button>(
        next.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 prev button then we obtain the other text in the TextView.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads