Free Education

Online Education

Android Studio

ImageSwitcher Tutorial With Example In Android Studio

Hello Guys. Welcome to our new tutorial of ImageSwitcher Tutorial With Example In Android Studio.basically imageswitcher is used to smooth transition animation effect to the image for switching one image to another.

please follow the code given below to how to use ImageSwitcher in android studio. for switch different images click on the Next button then the image will switch.

  • First Create a new project in Android Studio
  • File ⇒ New Android ⇒ Application Project
  • Then Open src -> package -> MainActivity.java and then add following code :

JAVA (MainActivity.java)

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 ImageSwitcher_activity extends AppCompatActivity {

    int[] nameList = {R.drawable.android,
            R.drawable.code,
            R.drawable.ic_chat,
            R.drawable.ic_gallery,
            R.drawable.ic_map,

    };

    private int index = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_switcher_activity);

        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(nameList[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 < nameList.length) ? index : 0;
                    if (imageSwitcher != null) {
                        imageSwitcher.setImageResource(nameList[index]);
                    }
                }
            });
        }
    }
}
  •  Now Open res -> layout -> activity_main.xml and then add following code :

XML (activity_main.xml)

<android.support.constraint.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"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageSwitcher" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_gravity="center"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *