Dynamic TextSwitcher in Kotlin
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:
- 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.
Modify activity_main.xml file
In this file, we use the TextSwitcher, Buttons and also set their attributes.
Update strings.xml file
Here, we update the name of the application using the string tag.
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>(R.id.textSwitcher)
Add the TextSwitcher in layout using this
val c_Layout = findViewById
(R.id.constraint_layout) //add textSwitcher in constraint layout c_Layout?.addView(textSwitcher)
Run as Emulator:
Click prev button then we obtain the other text in the TextView.