Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Android ImageSwitcher Using Kotlin With Example - Tutorialwing

Android ImageSwitcher Using Kotlin With Example

Hello Readers! In this post, we are going to learn about android imageSwitcher using kotlin in any android application. We will also learn about different attributes of android imageSwitcher that can be used to customise this widget.


Tutorialwing Kotlin ImageSwitcher Output Android ImageSwitcher Using Kotlin With Example

Tutorialwing Kotlin ImageSwitcher Output

Getting Started

Android ImageSwitcher can be defined as below –

ImageSwitcher is a widget that are used to flip between different images with some animations on it

Different Attributes of Android ImageSwitcher Widget

Some of the popular attributes of android ImageSwitcher inherited from ViewAnimator are –

Sr. XML Attributes Description
1 android:animateFirstView Specifies whether to animate the current view when view animation is first displayed
2 android:inAnimation Identifier for the animation to use when a view is shown
3 android:outAnimation Identifier for the animation to use when a view is hidden

Some of the popular attributes of android ImageSwitcher inherited from FrameLayout are –

Sr. XML Attributes Description
1 android:foregroundGravity Specifies the gravity to apply to the foreground drawable
2 android:measureAllChildren Specifies whether to measure all children or just those in VISIBLE or INVISIBLE state while measuring

Some of the popular attributes of android CalendarView inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animationCache Specifies whether layout animations should create a drawing cache for their children
2 android:clipToPadding Specifies whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.
3 android:layoutAnimation Specifies layout animation to be used when viewGroup is laid out for the first time
4 android:layoutMode Specifies the layout mode of the viewGroup
5 android:splitMotionEvents Specifies whether viewGroup should split MotionEvents to separate child views during touch event dispatch

Some of the popular attributes of android CalendarView inherited from View are –

Sr. XML Attributes Description
1 android:background Defines background of the view
2 android:backgroundTint Specifies tint to apply to the background drawable
3 android:backgroundTintMode Specifies blending mode used to apply the background tint
4 android:clickable Specifies whether this view clickable or not
5 android:elevation Specifies z-depth of the view
6 android:id Specifies unique id of the view
7 android:onClick Specifies what to do when this view is clicked
8 android:visibility Specifies visibility(VISIBLE, INVISIBLE or GONE) of the view

Example of Android ImageSwitcher Using Kotlin

At first, we will create android application. Then, we will use imageSwitcher using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ImageSwitcher. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use imageSwitcher using kotlin in the application.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">ImageSwitcher</string>
	<string name="next">Next</string>

3. Use ImageSwitcher Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>




In activity_main.xml file, we have defined button and imageSwitcher widgets. Now, we will access these widgets in java file to perform some operations on them.

4. Access ImageSwitcher Widget in Kotlin file

Open src/main/java/com.tutorialwing.imageswitcher/MainActivity.kt file and add below code into it.

package com.tutorialwing.imageswitcher

import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageSwitcher
import android.widget.ImageView

class MainActivity : AppCompatActivity() {

    private var nameList = intArrayOf(R.drawable.chinchilla, R.drawable.cod, R.drawable.crane, R.drawable.crocodile, R.drawable.deer,, R.drawable.dolphin, R.drawable.dove, R.drawable.eel, R.drawable.elephant, R.drawable.emu,

    private var index = 0

    override fun onCreate(savedInstanceState: Bundle?) {

        val imageSwitcher = findViewById<ImageSwitcher>(
            val imageView = ImageView(applicationContext)
            imageView.scaleType = ImageView.ScaleType.FIT_CENTER
            imageView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,



        val `in` = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
        imageSwitcher?.inAnimation = `in`

        val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
        imageSwitcher?.outAnimation = out

        val button = findViewById<Button>(
            index = if (++index < nameList.size) index else 0

You will need some drawable images for imageSwitcher in this application. You can click here to download images for this application.

In MainActivity.kt file, we have accessed imageSwitcher widget. Then, we have set factory for imageSwitcher that are used to provide views between which imageSwitcher flips. After that, we have set imageResources in the view. Then, we have set animations to be shown when images appear/disappear on/from the screen. At last, we have accessed button and set click listener to show next image in the imageSwitcher.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.imageswitcher"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android imageSwitcher using Kotlin.