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.
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.
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.
Different attributes of TextSwitcher widget
|android:id||Used to specify the id of the view.|
|android:onClick||Used to specify the action when this view is clicked.|
|android:background||Used to set the background of the view.|
|android:padding||Used to set the padding of the view.|
|android:visibilty||Used to set the visibility of the view.|
|android:inAnimation||Used to define the animation to use when view is shown.|
|android:outAnimation||Used to define the animation to use when view is hidden.|
|android:animateFirstView||Used 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, Buttons and also set their attributes.
Update strings.xml file
Here, we update the name of the application using the string tag.
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>(R.id.textSwitcher)
Run as Emulator:
Click next button then we obtain the other text in the TextView.