Wednesday, January 30, 2013

Android VIEW-SWITCHER example

The View-Switcher is a utility to switch between. sometimes we can  have a question that why a view-switcher when we have already a view-flipper. actually both inherits viewanimator  in android  but the difference is a view switcher has two views while a view flipper can have any numer of views.

Make a project with the name ViewSwitcher and have an activity with name MainActivity.java with the following given code


package com.jitesh.viewswitcher;

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

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                new AnimationUtils();
                // TODO Auto-generated method stub
                switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
                switcher.showNext();
            }
        });
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                new AnimationUtils();
                // TODO Auto-generated method stub
                switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
                switcher.showPrevious();
            }
        });
    }
}


beside this activity_main.xml should have a layout like the given below.

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:src="@drawable/one" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Switcher 2" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:src="@drawable/two" />
    </LinearLayout>

</ViewSwitcher>

you can also download the code from the link ViewSwitcher

No comments:

Post a Comment