kotlin
Copy
private fun changeImage() {
if (isFirstImage) {
binding.imageView.setImageResource(R.drawable.image2)
} else {
binding.imageView.setImageResource(R.drawable.image1)
}
isFirstImage = !isFirstImage
}
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="Demo Image" />
kotlin
Copy
binding.imageView.setImageResource(R.drawable.image1)
kotlin
Copy
binding.changeImageButton.setOnClickListener {
changeImage()
}