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.
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/constraint_layout"> <Button android:id="@+id/prev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="128dp" android:text="@string/prev" app:layout_constraintRight_toLeftOf="@id/next" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="32dp" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toEndOf="@+id/prev" app:layout_constraintTop_toTopOf="@+id/prev" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Update strings.xml file
Here, we update the name of the application using the string tag.
XML
<resources> <string name="app_name">DynamicTextSwitcherInKotlin</string> <string name="next">Next</string> <string name="prev">Prev</string></resources> |
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)
Kotlin
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundleimport android.view.Gravityimport android.view.animation.AnimationUtilsimport android.widget.Buttonimport android.widget.TextSwitcherimport android.widget.TextViewimport android.graphics.Colorimport 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?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) 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) textView.setTextColor(Color.BLUE) textView } textSwitcher.setText(languages[index]) val c_Layout = findViewById<ConstraintLayout>(R.id.constraint_layout) //add textSwitcher in constraint layout c_Layout?.addView(textSwitcher) 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>(R.id.prev) prev.setOnClickListener { index = if (index - 1 >= 0) index - 1 else 4 textSwitcher.setText(languages[index]) } // next button functionality val next = findViewById<Button>(R.id.next) next.setOnClickListener { index = if (index + 1 < languages.size) index + 1 else 0 textSwitcher.setText(languages[index]) } }} |
AndroidManifest.xml file
XML
<?xml version="1.0" encoding="utf-8"?>package="com.geeksforgeeks.myfirstkotlinapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity></application> </manifest> |
Run as Emulator:
Click prev button then we obtain the other text in the TextView.




.png)