VOOZH about

URL: https://www.geeksforgeeks.org/android/viewanimator-in-android-with-example/

⇱ ViewAnimator in Android with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ViewAnimator in Android with Example

Last Updated : 23 Jul, 2025

ViewAnimator is a very fascinating and useful feature as it switches between two or more views smoothly and mainly meant for animation features of the views on screens. It is the parent class of ViewFlipper and ViewSwitcher and the main distinction is it can switch between more than 2 views also. It is a subclass of FrameLayout Container.

Following is the way to define ViewAnimator:

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

... add subviews here ...

</ViewAnimator>

Animation means apparently only one view can be active at a moment and hence there are many important methods available to make the flow smooth.

Important Methods

Methods

Description

showNext()

The name of the method is self-explanatory. This is used to show the next view of ViewAnimator. Only one view can be active at the moment.

showPrevious()

The name of the method is self-explanatory. This is used to show the previous view of ViewAnimator. Only one view can be active at the moment.

addView(View child)

At run time, if we want to add a view, we can use this. Add the child view at run time in the ViewAnimator.

setInAnimation(in)Set the animation of the appearance of the object on the screen
setOutAnimation(out)

Opposite of  setInAnimation(). The previous one is removed by using an animation set with the setOutAnimation() method, and then places the new one using the animation set by the setInAnimation() method.

getCurrentView()Currently displayed child view of ViewAnimator.
getDisplayedChild()Index for current displayed child view of ViewAnimator.
getInAnimation()

Current animation used to animate a View that enters the screen can be got by this method. This method returns the animation that we set using the setInAnimation() method.

getOutAnimation()

Current animation used to animate a View that exits the screen can be got by this method. This method returns the out animation that we set using setoutAnimation() method.

removeAllViews()To remove all child views from the ViewGroup.
removeView(View view)

To remove the child view of ViewAnimator. We can do that bypassing the child view which we want to remove.

removeViewAt(int index)

 If there is a requirement like to remove a view at the specified position in the group, we can use this.

setDisplayedChild(int whichChild)To set the index of current displayed child view of ViewAnimator
setAnimateFirstView(boolean animate)

The current view should be animated the first time in the ViewAnimator can be displayed to either true or false value.

getAnimateFirstView()If we have set the current view animated to true/false.

Attributes of ViewAnimator

Attributes

Description

idTo uniquely identify a ViewAnimator.
animateFirstViewIf we want to set the current view as animated, we can have this attribute
inAnimationThe identifier for the animation to use when a view is shown
outAnimationThe identifier for the animation to use when a view is hidden
paddingset the padding from the left, right, the top, or bottom side of a ViewAnimator.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.

activity_main.xml:

Design UI:

👁 view-animator-design


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. 

MainActivity.kt:

Output:

Comment
Article Tags:
Article Tags:

Explore