Android ImageSwitcher Using Kotlin With Example

In this article, we will learn about android ImageSwitcher using Kotlin. We will go through various example that demonstrates how to use different attributes of ImageSwitcher. For example,

In this article, we will get answer to questions like –

  • What is ImageSwitcher?
  • Why should we consider ImageSwitcher while designing ui for any app?
  • What are possibilities using ImageSwitcher while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin ImageSwitcher Output Android ImageSwitcher Using Kotlin With Example

Tutorialwing Kotlin ImageSwitcher Output

Getting Started

We can define android ImageSwitcher widget as below –

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

Now, how do we use ImageSwitcher in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ImageSwitcher. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ImageSwitcher Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ImageSwitcher in Kotlin

Follow steps below to use ImageSwitcher in newly created project –

  • We will need some drawable images for imageSwitcher in this application. We can click here to download images for this application.
  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ImageSwitcher</string>
        <string name="next">Next</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:text="@string/next"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageSwitcher
            android:id="@+id/imageSwitcher"
            android:layout_width="320dp"
            android:layout_height="320dp"
            android:layout_gravity="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnNext" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

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

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.imageswitcher
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.animation.AnimationUtils
    import android.widget.FrameLayout
    import android.widget.ImageView
    import com.tutorialwing.imageswitcher.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	private var nameList = intArrayOf(
    		R.drawable.chinchilla,
    		R.drawable.cod,
    		R.drawable.crane,
    		R.drawable.crocodile,
    		R.drawable.deer,
    		R.drawable.dog,
    		R.drawable.dolphin,
    		R.drawable.dove,
    		R.drawable.eel,
    		R.drawable.elephant,
    		R.drawable.emu,
    		R.drawable.fly
    	)
    
    	private var index = 0
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupImageSwitcher()
    	}
    
    	private fun setupImageSwitcher() {
    		binding.imageSwitcher.setFactory {
    			val imageView = ImageView(applicationContext)
    			imageView.scaleType = ImageView.ScaleType.FIT_CENTER
    			imageView.layoutParams = FrameLayout.LayoutParams(
    				FrameLayout.LayoutParams.WRAP_CONTENT,
    				FrameLayout.LayoutParams.WRAP_CONTENT
    			)
    			imageView
    		}
    
    		binding.imageSwitcher.setImageResource(nameList[index])
    
    		val inAnim = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
    		binding.imageSwitcher.inAnimation = inAnim
    
    		val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
    		binding.imageSwitcher.outAnimation = out
    
    		binding.btnNext.setOnClickListener {
    			index = if (++index < nameList.size) index else 0
    			binding.imageSwitcher.setImageResource(nameList[index])
    		}
    	}
    }
    

    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.

Now, run the application. We will get output as below –

Tutorialwing Kotlin ImageSwitcher Output Android ImageSwitcher Using Kotlin With Example

Tutorialwing Kotlin ImageSwitcher Output

Different Attributes of ImageSwitcher in XML

Now, we will see how to use different attributes of Android ImageSwitcher using Kotlin to customise it –

Set Id of ImageSwitcher

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ImageSwitcher using android:id attribute like below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        />

Here, we have set id of ImageSwitcher as imageSwitcher_ID using android:id=”” attribute. So, if we need to reference this ImageSwitcher, we need to use this id – imageSwitcher_ID.
Learn to Set ID of ImageSwitcher Dynamically

Set Width of ImageSwitcher

We use android:layout_width=”” attribute to set width of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ImageSwitcher Dynamically

Set Height of ImageSwitcher

We use android:layout_height=”” attribute to set height of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ImageSwitcher Dynamically

Set Padding of ImageSwitcher

We use android:padding=”” attribute to set padding of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ImageSwitcher using android:padding=”” attribute.
Learn to Set Padding of ImageSwitcher Dynamically

Set Margin of ImageSwitcher

We use android:layout_margin=”” attribute to set margin of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ImageSwitcher using android:layout_margin=”” attribute.
Learn to Set Margin of ImageSwitcher Dynamically

Set Background of ImageSwitcher

We use android:background=”” attribute to set background of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ImageSwitcher using android:background=”” attribute.
Learn to Set Background of ImageSwitcher Dynamically

Set Visibility of ImageSwitcher

We use android:visibility=”” attribute to set visibility of ImageSwitcher.
We can do it as below –

    <ImageSwitcher
        android:id="@+id/imageSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ImageSwitcher using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ImageSwitcher Dynamically

Till now, we have see how to use android ImageSwitcher using Kotlin. We have also gone through different attributes of ImageSwitcher to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android ImageSwitcher Widget

Below are the various attributes that are used to customise android ImageSwitcher Widget. However, you can check the complete list of attributes of ImageSwitcher in it’s official documentation site. Here, we are going to list some of the important attributes of this 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

We have seen different attributes of ImageSwitcher and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ImageSwitcher, how can we use android ImageSwitcher using Kotlin ? etc. We also went through different attributes of android ImageSwitcher.

Leave a Reply