package
com.geeksforgeeks.myfirstkotlinapp
import
androidx.appcompat.app.AppCompatActivity
import
android.os.Bundle
import
android.view.Gravity
import
android.view.animation.AnimationUtils
import
android.widget.Button
import
android.widget.TextSwitcher
import
android.widget.TextView
import
android.graphics.Color
import
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)
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
val prev = findViewById<Button>(R.id.prev)
prev.setOnClickListener {
index =
if
(index -
1
>=
0
) index -
1
else
4
textSwitcher.setText(languages[index])
}
val next = findViewById<Button>(R.id.next)
next.setOnClickListener {
index =
if
(index +
1
< languages.size) index +
1
else
0
textSwitcher.setText(languages[index])
}
}
}