Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
ViewAnimator in Android with Example
Next article icon

ViewAnimator in Android with Example

Last Updated : 25 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/main"     android:gravity="center"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">      <ViewAnimator         android:id="@+id/simpleViewAnimator"         android:layout_width="match_parent"         android:layout_height="350dp">     </ViewAnimator>      <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="32dp"         android:text="Next" />  </LinearLayout> 

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:

Kotlin
package org.geeksforgeeks.demo  import android.os.Bundle import android.view.animation.AnimationUtils import android.widget.Button import android.widget.ImageView import android.widget.ViewAnimator import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      private lateinit var viewAnimator: ViewAnimator     private lateinit var button: Button     private var images = intArrayOf(R.drawable.k1,             R.drawable.k2, R.drawable.k3, R.drawable.k4)      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          button = findViewById(R.id.button)         viewAnimator = findViewById(R.id.simpleViewAnimator)          for (i in images.indices) {                      // create a new object  for ImageView by this way             val imgView = ImageView(this)             imgView.scaleType = ImageView.ScaleType.CENTER_CROP              // Let us set image resource for ImageView             imgView.setImageResource(images[i])              // Then add the child view in ViewAnimator             viewAnimator.addView(imgView)         }          // Declare in and out animations and load         // them using AnimationUtils class         val animationIn = AnimationUtils.loadAnimation(this, android.R.anim.fade_in)         val animationOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out)          // set the animation type to ViewAnimator         viewAnimator.inAnimation = animationIn         viewAnimator.outAnimation = animationOut          button.setOnClickListener {             // show the next view of ViewAnimator             viewAnimator.showNext()         }     } } 

Output:


Next Article
ViewAnimator in Android with Example

P

priyarajtt
Improve
Article Tags :
  • Kotlin
  • Android
  • Android-View

Similar Reads

    ObjectAnimator in Android with Example
    ObjectAnimator is a Subclass of ValueAnimator, which allows us to set a target object and object property to animate. It is an easy way to change the properties of a view with a specified duration. We can provide the end position and duration of the animation. Let us create a simple version of the G
    5 min read
    ViewSwitcher in Android with Example
    All android apps will have a feature to switch different views in order to explain/promote their site or product. Visual representation of a product by showing different views will impress the customers easily. In this article, let us see how to bring the "ViewSwitcher" to Android. ViewSwitcher is a
    6 min read
    SimpleAdapter in Android with Example
    In Android, whenever we want to bind some data which we get from any data source (e.g. ArrayList, HashMap, SQLite, etc.) with a UI component(e.g. ListView, GridView, etc.) then Adapter comes into the picture. Basically Adapter acts as a bridge between the UI component and data sources. Here Simple A
    7 min read
    TextView in Android with Example
    TextView is a simple widget that is seen in every android application. This widget is used to display simple text within the android application. We can add custom styling to the text that we have to show. In this article, we will take a look at How to create a simple Text View in an android applica
    2 min read
    Spinner in Android with Example
    Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences