Android ViewAnimator with Animation Example Tutorial

How to flip slide multiple views in android screen one by one using next previous buttons with coding.

ViewAnimator widget is mostly same as ViewSwitcher widget but there is only one major difference between both of them that is ViewSwitcher can only contain two widgets and more then two widgets is not allowed in it but ViewAnimator can contain as many as its developer wants widgets in it and with its next previous buttons developer can easily switch between widgets. so here is the complete step by step tutorial for Android ViewAnimator with Animation Example Tutorial.

android-project-download-code-button

Live Demo :

Android ViewAnimator with Animation Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.viewanimator_android_examplescom;
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.ViewAnimator;

public class MainActivity extends AppCompatActivity {

    ViewAnimator viewAnimator ;
    Button buttonN , buttonP ;
    Animation animationIn, animationOut ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewAnimator = (ViewAnimator)findViewById(R.id.viewAnimator1);
        buttonN = (Button)findViewById(R.id.button2);
        buttonP = (Button)findViewById(R.id.button);

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

        viewAnimator.setInAnimation(animationIn);
        viewAnimator.setOutAnimation(animationOut);

        buttonP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                viewAnimator.showPrevious();

            }
        });


        buttonN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                viewAnimator.showNext();

            }
        });

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.viewanimator_android_examplescom.MainActivity"
    android:background="#FFF8E1">


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


        <Button
            android:text="Button inside ViewAnimator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

        <ImageView
            android:src="@drawable/sample_viewanimator_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/imageview1" />

        <TextView
            android:text="This is the TextView Widget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/textView2"
            android:textSize="25dp"
            android:gravity="center"
            android:textColor="#000000"/>



    </ViewAnimator>

    <Button
        android:text="Previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button" />

    <Button
        android:text="Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button2" />

</RelativeLayout>

Screenshots:

viewanimator-3

 

viewanimator-1

 

Android ViewAnimator with Animation Example Tutorial

Click here to download Android ViewAnimator with Animation Example Tutorial project with source code.