Android ViewAnimator

ViewAnimator can be defined as below –

"ViewAnimator is a widget that are used to switch between views using animations"

Some of the popular Android ViewAnimator attributes are –

 

 

S. No. XML Attributes Descriptions
1 android:inAnimation Identifier for the animation to use when a view is shown.
2 android:outAnimation Identifier for the animation to use when a view is hidden.
3 android:animateFirstView Defines whether to animate current view when the viewAnimation is first displayed.

Some of the popular android ViewAnimator attributes inherited from FrameLayout are-

S. No. XML Attributes Descriptions
1 android:measureAllChildren Defines whether to measure all children or just those in VISIBLE or INVISIBLE state when measuring.
2 android:foregroundGravity Defines the gravity to apply for foreground drawable.

 

Some of the popular Android ViewAnimator attributes inherited from ViewGroup are –

S. No. XML Attributes Decriptions
1 android:layoutMode Defines layout mode of this viewGroup.
2 android:persistentDrawingCache Defines persistence of the drawing cache.
3 android:layoutAnimation Defines the layout animation to be used when this viewGroup is laid out for the first time.
4 android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.
5 android:splitMotionEvents Defines this viewGroup should split MotionEvents to separate child views during touch event dispatch.

 

 

Some of the popular ViewAnimator android attributes inherited from View are –

S. No. XML Attributes Descriptions
1 android:id This is unique id of the ViewAnimator to uniquely identify the ViewAnimator.
2 android:hight Height of the ViewAnimator.
3 android:width Width of the ViewAnimator.
4 android:onClick Defines what to do when this view is clicked.
5 android:clickable Defines whether this view is clickable or not.
6 android:padding Defines padding of the view.
7 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view.
8 android:background Defines drawable of the background.
9 android:backgroundTint Defines tint to apply to the background.
10 android:backgroundTintMode Defines blending mode to be used to apply tint to the background.

We'll create android application at first. Then in this application we will use the ViewAnimator.

To create new project, follow steps below. If you already created a new application, please ignore the steps.

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project then choose your project and select Empty Activity.
3 Write application name as ViewAnimator after this write a package name you can write your own package name.
4 Select language Java. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial  to Create a New Project to know steps in detail.

Now, we are going to modify xml and java file to use the ViewAnimator in the application.

 

 

Open res/values/strings.xml file. Then, add below code into it.

<resources>
<string name="app_name">ViewAnimator</string>
<string name="next">Next</string>
<string name="prev">Previous</string>
<string name="item_post_1">Text at position 1</string>
<string name="item_post_2">Button at position 2</string>
<string name="item_post_4">Text at positon 4</string>

</resources>

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">

<Button
android:id="@+id/btnPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prev"/>

<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"/>
</LinearLayout>

<ViewAnimator android:id="@+id/viewAnimator"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/item_post_1"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"/>

<Button
 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/item_post_2">

</Button>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/item_post_4"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"/>
 
</ViewAnimator>

</LinearLayout>

We described ViewAnimator in the activity main.xml file. We've also defined the views between which we will switch using this.Here, we've four views Textview, Button, ImageView and TextView in order with position between which animator will switch. animator. Now we're going to access this animator in java file to do some activities in it. 

Open src/main/java/com.ukacademe.viewanimator/MainActivity.java file. Then, add below code into it.

package com.ukacademe.viewanimator;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ViewAnimator animator = findViewById(R.id.viewAnimator);

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

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

        Button buttonPrev = findViewById(R.id.btnPrev);
        buttonPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animator.showPrevious();
            }
        });

        Button buttonNext = findViewById(R.id.btnNext);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animator.showNext();
            }
        });
    }
}

In MainActivity.java file, we have defined ViewAnimator in xml file. Then, we have set in and out animation in it that are used when current view is replaced by view just after/before it. After that, we have set click listener in button that will switch view in viewAnimator with next or previous item in ViewAnimator.

Since AndroidManifest.xml file is very important in any android application, we'll also see the content in this file.

Code inside app/manifests/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.viewanimator">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

When we run the program, we'll get output as shown below.