Using ViewSwitcher in Android

In this blog post, I would like to show how to use ViewSwitcher in Android.

1. Create a new project File -> Android Project. On MainActivity.java and copy paste this code:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

 Button buttonPrevious, buttonNext;
 ViewSwitcher viewSwitcher;

 Animation slide_in_left, slide_out_right;

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

 buttonPrevious = (Button) findViewById(R.id.prev);
 buttonNext = (Button) findViewById(R.id.next);
 viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);

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

 viewSwitcher.setInAnimation(slide_in_left);
 viewSwitcher.setOutAnimation(slide_out_right);

 buttonPrevious.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View arg0) {

 Toast.makeText(getApplicationContext(), "Showing previous view..", Toast.LENGTH_SHORT).show();
 viewSwitcher.showPrevious();
 }
 });

 buttonNext.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View arg0) {

 Toast.makeText(getApplicationContext(), "Showing next view..", Toast.LENGTH_SHORT).show();
 viewSwitcher.showNext();
 }
 });
 }
}

2. Now open your main.xml layout and copy paste this code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 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=".MainActivity" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:autoLink="web"
 android:text="@string/app_name"
 android:textStyle="bold" />

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

 <Button
 android:id="@+id/prev"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="previous" />

 <Button
 android:id="@+id/next"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="next" />
 </LinearLayout>

 <ViewSwitcher
 android:id="@+id/viewswitcher"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >

 <ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:src="@drawable/ic_launcher" />

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:orientation="vertical" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center_horizontal"
 android:text="sourcecodez.net" />
 </LinearLayout>
 </ViewSwitcher>

</LinearLayout>

ScreenShots:

Screenshot_2014-10-04-20-09-19 Screenshot_2014-10-04-20-07-27

Tips to Remember Always:

Android View Switcher class is used, as its name suggests to switch between views. And here by switch we mean there can only be two views. If one more view is added the app would crash.

 

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

Download Source Code from here.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

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