Android ImageSwitcher

ImageSwitcher can be defined as below –

"Android ImageSwitcher provides an image animation to transition from one image to another."

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

S. No. XML Attributes Descriptions
1 android:id This is unique id of the ImageSwitcher to uniquely identify the ImageSwitcher.
2 android:height Height of the ImageSwitcher.
3 android:width Width of the ImageSwitcher.
4 android:background Defines background of the view.
5 android:backgroundTint Defines tint to apply to the background drawable.
6 android:backgroundTintMode Defines blending mode used to apply the background tint.
7 android:clickable Defines whether this view clickable or not.
8 android:elevation Defines z-depth of the view.
9 android:onClick Defines what to do when this view is clicked.
10 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view.



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

S. No. XML Attributes Decriptions
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 ImageSwitcher inherited from ViewAnimator are –

S. No. XML Attributes Descriptions
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 –

S. No. XML Attributes Descripitions
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.



At first, we will create android application. Then, we will use ImageSwitcher in the application.

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

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project Write application name as ImageSwitcher. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 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 tutorial  to Create a New Project to know steps in detail.

Now we're going to modify xml and java file to use the ImageSwitcher in the application.

<string name="app_name">ImageSwitcher</string>
<string name="next">Next</string>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""


android:text="@string/next" />


We have described Button and ImageSwitcher in activity main.xml. Now, in the java file, we will access these widgets and execute some activities on them.

Open app/java/com.ukacademe.imageswitcher/ file. Then, add below code into it.

package com.ukacademe.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[] CourseList = {R.drawable.android_ukacademe,


    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 < CourseList.length) ? index : 0;
                    if (imageSwitcher != null) {

We use ImageSwitcher and Button in file. Then we set factory for ImageSwitcher. This is used to create views between imageSwitcher flips. Here we create ImageViews and set the parameters. After that, we set the default image using the ImageResource method. After that, when they begin appearing/disappearing on/from the screen, we set animations for pictures. 

We finally set a click listener to the button to display the next image in the ImageSwitcher.

Note that the first image will be displayed if we are at the last image and the next button is pressed.

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

Code inside app/manifests/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

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

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


When we run the application, we will get output as shown below.