Watch Video Tutorial
Step 1 :
- Create New Project With EmptyActivity
- Add TextSwitcher And Button
- copy paste this code
private val textList = arrayOf("Panda", "Tiger", "Zebra", "Lion", "Deer", "Goat", "Ape", "Monkey", "Human")
private var index = 0
val textSwitcher = findViewById<TextSwitcher>(R.id.textSwitcher)
textSwitcher.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 32f
textView.setTextColor(Color.RED)
textView
}
textSwitcher.setText(textList[index])
val `in` = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
textSwitcher.inAnimation = `in`
val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
textSwitcher.outAnimation = out
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
index = if (index + 1 < textList.size) index + 1 else 0
textSwitcher.setText(textList[index])
}