Image Switcher with example and Source code

The ImageSwitcher methods give you a simple way to add animated transitions .ImageSwitcher are used to have an smooth transition animation in android view. Imagine you need to cycle through information  in an ImageView.  Some examples of this would be
  • Displaying ABCD letter images
  • Displaying fruits in sequence
  • Display House construction images
  • etc
Main Activity: ImageSwitcherExampleActivity.java
package com.successmobigroup.imageswitcherexample;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.widget.Button;

import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
import com.successmobigroup.imageswitcherexample.R;

public class ImageSwitcherExampleActivity extends Activity implements ViewFactory{
  ImageSwitcher is;
  int [] imgid = {R.drawable.android1,R.drawable.android2,R.drawable.android3,R.drawable.android4};
  Button prev, next;
  int count =0;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_image_switcher_example);
  
  is = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
  prev = (Button)findViewById(R.id.button1);
  next = (Button)findViewById(R.id.button2);
  is.setFactory(this);
  is.setInAnimation(this, android.R.anim . slide_in_left);
  is.setOutAnimation(this, android.R.anim.slide_out_right);
 
  prev.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
     if(count>0)
     {
      count--;
      try{
        is.setImageResource(imgid[count]);
       }
      catch(Exception e)
      {
       e.printStackTrace();
      }
     }
     else
     {
      Toast.makeText(ImageSwitcherExampleActivity.this, "First", Toast.LENGTH_LONG).show();
     }
   }
  });
  next.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(count<imgid.length)
    {
     
         
     try{
      is.setImageResource(imgid[count]);
     }
    catch(Exception e)
    {
     e.printStackTrace();
    }
     count++;
     
    }
    else
    {
     Toast.makeText(ImageSwitcherExampleActivity.this, "Last", Toast.LENGTH_LONG).show();
    }
   }
  });
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.image_switcher_example, menu);
  return true;
 }

 @Override
 public View makeView() {
  // TODO Auto-generated method stub
  ImageView iv = new ImageView(this);
  iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
  iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  
  return iv;
 }

}
Layout xml: activity_image_switcher_example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ImageSwitcherExampleActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageSwitcher
            android:id="@+id/imageSwitcher1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ImageSwitcher>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="&lt;&lt;" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=">>" />

    </LinearLayout>

</LinearLayout>

Download source Complete source code here:

(right click save as)

Steps to Open Source in Eclipse:

1.Download Zip from above link
2.Locate the ZIP and extract
3.In Eclipse
  File-->Import-->Existing Android Code Into Workspace-->Browse
4.Locate file and say next
5.Done, You will find folder in your workspace

Screen Shots: