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.

<resources>
<string name="app_name">ImageSwitcher</string>
<string name="next">Next</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageSwitcher
android:layout_marginTop="150dp"
android:id="@+id/imageSwitcher"
android:layout_width="320dp"
android:layout_height="320dp"
android:layout_gravity="center"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:text="@string/next" />

</LinearLayout>

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/MainActivity.java file. Then, add below code into it.

package com.ukacademe.imageswitcher;

import android.support.v7.app.AppCompatActivity;
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,
            R.drawable.aspnet_ukcademe,
            R.drawable.csharp_ukcademe,
            R.drawable.css_ukacademe,
            R.drawable.html_ukacademe,
            R.drawable.java_ukacademe,
            R.drawable.javascript_ukacademe,

    };

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageSwitcher imageSwitcher = findViewById(R.id.imageSwitcher);
        if (imageSwitcher != null) {
            imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
                public View makeView() {
                    ImageView imageView = new ImageView(getApplicationContext());
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    imageView.setLayoutParams(new
                            ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    return imageView;
                }
            });

            imageSwitcher.setImageResource(CourseList[index]);

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

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

        Button button = findViewById(R.id.button);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    index = (++index < CourseList.length) ? index : 0;
                    if (imageSwitcher != null) {
                        imageSwitcher.setImageResource(CourseList[index]);
                    }
                }
            });
        }
    }
}

We use ImageSwitcher and Button in MainActivity.java 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="http://schemas.android.com/apk/res/android"
package="com.ukacademe.imageswitcher">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

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