Using ViewAnimator in Android

In this blog post, I would like to show how to use ViewAnimator 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.MotionEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ViewAnimator;

public class MainActivity extends Activity {

 ViewAnimator mContainer;

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

 initContainer();
 }

 private void initContainer() {
 mContainer = (ViewAnimator)findViewById(R.id.viewAnimator1);

 Animation inAnim = new AlphaAnimation(0, 1);
 inAnim.setDuration(1000);
 Animation outAnim = new AlphaAnimation(1, 0);
 outAnim.setDuration(1000);

 mContainer.setInAnimation(inAnim);
 mContainer.setOutAnimation(outAnim);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 if(event.getAction() == MotionEvent.ACTION_UP) {
 mContainer.showNext();
 }
 return true;
 }

}

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

<RelativeLayout 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: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="net.sourcecodez.viewanimator.MainActivity" >

 <ViewAnimator
 android:id="@+id/viewAnimator1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="100dp"
 android:src="@drawable/ic_launcher" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="100dp"
 android:textSize="20dp"
 android:text="TextView" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="100dp"
 android:text="Button" />

 </ViewAnimator>

</RelativeLayout>

ScreenShots:

Screenshot_2014-10-04-20-07-08 Screenshot_2014-10-04-20-06-57 Screenshot_2014-10-04-20-06-44

 

Tips to Remember Always:

ViewAnimator is ViewGroup. Actually it is extended from FrameLayout. So it could contain other views. But at any moment ViewAnimator can show no more than one view. We can ask ViewAnimator to switch to another view by calling showNext(), showPrevious() as well as setDisplayedChild(viewIndex) methods. And ViewAnimator is absolutely indifferent what kind of view it contains. ViewFlipper and ViewSwitcher are its subclasses.

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 *