package com.geeksforgeeks.myfirstkotlinapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageSwitcher
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
private val flowers = intArrayOf(R.drawable.flower1, R.drawable.flower2,
R.drawable.flower4)
private var index = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imgSwitcher = ImageSwitcher(this)
imgSwitcher?.setFactory({
val imgView = ImageView(applicationContext)
imgView.scaleType = ImageView.ScaleType.FIT_CENTER
imgView.setPadding(20, 20, 20, 20)
imgView
})
val c_Layout = findViewById<ConstraintLayout>(R.id.constraint_layout)
c_Layout?.addView(imgSwitcher)
imgSwitcher?.setImageResource(flowers[index])
val imgIn = AnimationUtils.loadAnimation(
this, android.R.anim.slide_in_left)
imgSwitcher?.inAnimation = imgIn
val imgOut = AnimationUtils.loadAnimation(
this, android.R.anim.slide_out_right)
imgSwitcher?.outAnimation = imgOut
val prev = findViewById<Button>(R.id.prev)
prev.setOnClickListener {
index = if (index - 1 >= 0) index - 1 else 1
imgSwitcher?.setImageResource(flowers[index])
}
val next = findViewById<Button>(R.id.next)
next.setOnClickListener {
index = if (index + 1 < flowers.size) index +1 else 0
imgSwitcher?.setImageResource(flowers[index])
}
}
}