VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/dynamic-textswitcher-in-kotlin/

⇱ Dynamic TextSwitcher in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic TextSwitcher in Kotlin

Last Updated : 7 Feb, 2025

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.

Steps of Implementing Dynamic TextSwitcher

Step 1: Create New Project

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

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

Step 2: Modify activity_main.xml file

In this file, we only use two Buttons and no TextSwitcher as we will add it dynamically.

activity_main.xml:

Design UI:

👁 design-ui-dynamic-textswitcher

Step 3: Create TextSwitcher in MainActivity.kt file

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

private val array = arrayOf("1","2","3","4","5")

then, we create the TextSwitcher and set attributes for textView like text color, text Size.

val textSwitcher: TextSwitcher = findViewById(R.id.textSwitcher)

Add the TextSwitcher in layout using this

val layout: ConstraintLayout = findViewById(R.id.main)
layout.addView(textSwitcher)

MainActivity.kt:

Output:

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


Comment
Article Tags:

Explore