Access ImageSwitcher in MainActivity.kt file
First, we declare an array flowers which contains the resource of the images used for the ImageView.
private val flowers = intArrayOf(R.drawable.flower1, R.drawable.flower2, R.drawable.flower4)
then, we access the ImageSwitcher from the XML layout and set ImageView to display the image.
val imgSwitcher = findViewById<ImageSwitcher>(R.id.imgSw) imgSwitcher?.setFactory({ val imgView = ImageView(applicationContext) imgView.scaleType = ImageView.ScaleType.FIT_CENTER imgView.setPadding(8, 8, 8, 8) imgView })
Also, we will use one of the above method for ImageSwitcher.
imgSwitcher?.setImageResource(flowers[index])
Kotlin
package com.w3wiki.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 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) // access the ImageSwitcher val imgSwitcher = findViewById<ImageSwitcher>(R.id.imgSw) imgSwitcher?.setFactory({ val imgView = ImageView(applicationContext) imgView.scaleType = ImageView.ScaleType.FIT_CENTER imgView.setPadding( 8 , 8 , 8 , 8 ) imgView }) // set the method and pass array as a parameter 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 // previous button functionality val prev = findViewById<Button>(R.id.prev) prev.setOnClickListener { index = if (index - 1 >= 0 ) index - 1 else 2 imgSwitcher?.setImageResource(flowers[index]) } // next button functionality val next = findViewById<Button>(R.id.next) next.setOnClickListener { index = if (index + 1 < flowers.size) index + 1 else 0 imgSwitcher?.setImageResource(flowers[index]) } } } |
ImageSwitcher in Kotlin
Android ImageSwitcher is a user interface widget that provides a smooth transition animation effect to the images while switching between them to display in the view.
ImageSwitcher is subclass of View Switcher which is used to animates one image and displays next one.
Generally, we use ImageSwitcher in two ways manually in XML layout and programmatically in Kotlin file.
We should define an XML component, to use ImageSwitcher in our android application.
XML
< ImageSwitcher android:id = "@+id/imgSw" android:layout_width = "match_parent" android:layout_height = "match_parent" > </ ImageSwitcher > |
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.
Contact Us