Android ImageSwitcher Tutorial With Example

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


Tutorialwing Android ImageSwitcher Output Android ImageSwitcher Tutorial With Example

Tutorialwing Android ImageSwitcher Output

Getting Started

ImageSwitcher widget can be defined as below –

An ImageSwitcher is a widget that allows you to transition from one image to another with some animations on images.

Attributes of Android ImageSwitcher Widget

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

Sr. XML Attributes Description
1 android:animateFirstView Defines 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 Defines 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 Defines whether layout animations should create a drawing cache for their children
2 android:clipToPadding Defines 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 Defines layout animation to be used when viewGroup is laid out for the first time
4 android:layoutMode Defines the layout mode of the viewGroup
5 android:splitMotionEvents Defines 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 Defines tint to apply to the background drawable
3 android:backgroundTintMode Defines blending mode used to apply the background tint
4 android:clickable Defines whether this view clickable or not
5 android:elevation Defines z-depth of the view
6 android:id Defines unique id of the view
7 android:onClick Defines what to do when this view is clicked
8 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view

Example of Android ImageSwitcher Widget

At first, we will create android application. Then, we will use imageSwitcher widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ImageSwitcher. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use imageSwitcher widget 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 res/layout/activity_main.xml file. Then, add below code into it.

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




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

4. Access ImageSwitcher Widget in java file

Open src/main/java/com.tutorialwing.imageswitcher/ file. Then, add below code into it.

package com.tutorialwing.imageswitcher;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

	int[] nameList = {R.drawable.chinchilla,

	private int index = 0;

	protected void onCreate(Bundle savedInstanceState) {

		final ImageSwitcher imageSwitcher = findViewById(;
		if (imageSwitcher != null) {
			imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
				public View makeView() {
					ImageView imageView = new ImageView(getApplicationContext());

					return imageView;


			Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

			Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

		Button button = findViewById(;
		if (button != null) {
			button.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					index = (++index < nameList.length) ? index : 0;
					if (imageSwitcher != null) {

At first, you need to store some images in res/drawable folder. However, you can click here to download drawables used in this project.

In file, we have accessed imageSwitcher and button widgets. Then, we have set factory for imageSwitcher. This is used to created views between imageSwitcher will flip. Here, we are creating imageViews and setting it’s parameters. After that, we have set default image using imageResource method. After that, we have set animations for images when they start appearing/disappearing on/from the screen. At last, we have set a click listener on button to show next image in the imageSwitcher. Note that first image will be shown if we are currently at last image and next button is clicked.

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


Code inside src/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 tutorial on Android ImageSwitcher widget.

Leave a Reply