Android ViewAnimator Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android ViewAnimator widget in any android application. We will also go through different attributes of ViewAnimator widget that can be used to customise it.

Output

Tutorialwing Android ViewAnimator Output Android ViewAnimator Tutorial With Example

Tutorialwing Android ViewAnimator Output

Getting Started

ViewAnimator widget can be defined as below –

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

Attributes of Android ViewAnimator Widget

Some of the popular attributes of android ViewAnimator widget are –

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

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

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

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

Sr. XML Attributes Description
1 android:splitMotionEvents Defines this viewGroup should split MotionEvents to separate child views during touch event dispatch
2 android:persistentDrawingCache Defines persistence of the drawing cache
3 android:layoutMode Defines layout mode of this viewGroup
4 android:layoutAnimation Defines the layout animation to be used when this viewGroup is laid out for the first time
5 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.

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

Sr. XML Attributes Description
1 android:background Defines drawable of the background
2 android:backgroundTint Defines tint to apply to the background
3 android:backgroundTintMode Defines blending mode to be used to apply tint to the background
4 android:clickable Defines whether this view is clickable or not
5 android:id Defines unique id of the view
6 android:onClick Defines what to do when this view is clicked
7 android:padding Defines padding of the view
8 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view



Example of Android ViewAnimator Widget

At first, we will create android application. Then, we will use ViewAnimator widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ViewAnimator. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use ViewAnimator widget in the application.

2. Modify Values folder

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>

3. Use ViewAnimator Widget in xml file

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/buttonPrev"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/prev"/>

		<Button
			android:id="@+id/buttonNext"
			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>

In activity_main.xml file, we have defined ViewAnimator. We have also defined the views between which we will switch using this animator. Here, we have four views Textview, Button, ImageView and TextView in order with position between which animator will switch. Now, we will access this animator in java file to do some operations in it.

4. Access ViewAnimator Widget in java file

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

package com.tutorialwing.viewanimator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.buttonPrev);
		buttonPrev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				animator.showPrevious();
			}
		});

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

In MainActivity.java file, we have accessed ViewAnimator defined 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 are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

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

	<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 will get output as shown above.

That’s end of tutorial on Android ViewAnimator widget.

Leave a Reply