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
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
How to add ViewFlipper in android?
Next article icon

How to add ViewFlipper in android?

Last Updated : 18 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
This article is about the implementation of ViewFipper in android. It is an extension of the ViewAnimator class which helps to animate between views added to it. ViewFlipper makes it easy to switch view. To control over flipping between views ViewFlipper provides two methods startFlipping() and stopFlipping(). To automatically switch between views, add the autoStart tag and set its value to true.To give more control to user, add views dynamically in the ViewFlipper. A ViewFlipper can be used in the gallery app to navigate between the images or videos. Approach:
  1. Create a new Android Resource Directory. Right-click on res folder and select Android Resource Directory. Make sure to select resource type as anim.
  2. Now create a new slide_left.xml file in the anim directory and add the following code. This is the animation that will be used in switching views. slide_left.xml
         <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <translate         android:duration="@android:integer/config_mediumAnimTime"         android:fromXDelta="0"         android:toXDelta="-50%p" />     <alpha         android:duration="@android:integer/config_mediumAnimTime"         android:fromAlpha="1.0"         android:toAlpha="0.0" /> </set>      
  3. Now create a new slide_right.xml file in the anim directory and add the following code. This is the animation that will be used be used in switching views. slide_right.xml
         <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <translate         android:duration="@android:integer/config_mediumAnimTime"         android:fromXDelta="50%p"         android:toXDelta="0" />     <alpha         android:duration="@android:integer/config_mediumAnimTime"         android:fromAlpha="0.0"         android:toAlpha="1.0" /> </set> 
  4. Add the following code in the activity_main.xml file. In this file, ViewFlipper is added to the layout. All the widgets that are added in the view flipper will act as different views. Two buttons next and previous are also added. activity_main.xml
         <?xml version="1.0" encoding="utf-8"?> <RelativeLayout      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:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="MainActivity">      <ViewFlipper          android:id="@+id/view_flipper"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:inAnimation="@android:anim/slide_in_left"         android:outAnimation="@android:anim/slide_out_right">          <ImageView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:src="@drawable/gfg" />          <TextView             android:layout_gravity="center"             android:textStyle="bold"             android:textColor="#219806"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="GeeksForGeeks"             android:textSize="20sp"         />          <Button             android:textColor="#219806"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:text="Open Website" />     </ViewFlipper>      <Button         android:id="@+id/prev_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_alignParentStart="true"         android:layout_margin="16dp"         android:text="Previous" />      <Button         android:id="@+id/next_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_alignParentEnd="true"         android:layout_margin="16dp"         android:text="Next" />  </RelativeLayout> 
  5. Now add the following code in the MainActivity.java file. Previous and Next buttons will help us to switch between the views. In previous button, for in animation slide_right is used and for out animation slide_left is used and vice versa for next button. MainActivity.java
    package org.geeksforgeeks.gfgviewflipper;  import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ViewFlipper; import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {     ViewFlipper flipper;     Button prev_Button, next_Button;      @Override     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          flipper = findViewById(R.id.view_flipper);         prev_Button = findViewById(R.id.prev_button);         next_Button = findViewById(R.id.next_button);          prev_Button.setOnClickListener(             new View.OnClickListener() {                 @Override                 public void onClick(View v)                 {                     // It is used to set the in and out                     // animation of View flipper.                     flipper.setInAnimation(MainActivity.this,                                            R.anim.slide_right);                     flipper.setOutAnimation(MainActivity.this,                                             R.anim.slide_left);                      // It shows previous item.                     flipper.showPrevious();                 }             });          next_Button.setOnClickListener(             new View.OnClickListener() {                 @Override                 public void onClick(View v)                 {                     // It is used to set the in and out                     // animation of View flipper.                     flipper.setInAnimation(MainActivity.this,                                            android.R.anim.slide_left);                     flipper.setOutAnimation(MainActivity.this,                                             android.R.anim.slide_right);                      // It shows next item.                     flipper.showNext();                 }             });     } } 
Output:
Add autoStart tag in ViewFlipper and set its value to true. Then it will work like this.

Next Article
How to add ViewFlipper in android?

M

madhavmaheshwarimm20
Improve
Article Tags :
  • Java
  • Android
  • DSA
  • Android-View
Practice Tags :
  • Java

Similar Reads

    How to Add WaveLineView in Android?
    In this article,  WaveLineView is implemented in android. WaveLineView provides us with a very beautiful UI. It can be used when the user has to wait for some time. WaveLineView makes our layout very attractive and hence enhancing the user experience of the app. WaveLineView provide two methods star
    3 min read
    How to add KenBurns View in Android?
    In this article, we will learn about how to add KenBurns View in android using java. KenBurns View is a useful library that is an extension of ImageView. It creates an immersive experience by animating its Drawable. We can use RandomTransitionGenerator to change the duration and acts as a interpolat
    2 min read
    ViewFlipper in Android with Kotlin
    View Flipper is a UI component in Android which is used to flip the view within the screen in the android application. The View Flipper class is an extension of ViewAnimator class. With the help of this, we can simply flip the views. In this article, we will take a look at How we can use ViewFlipper
    4 min read
    How to add Slidr Library in Android?
    In this layout, we will learn to add Slidr Library in android. This library is used to create attractive animation when user switch from one activity to another. This library can be used with both Activity and Fragment. It is very easy to implement. When Slidr is attached to the activity its interfa
    3 min read
    Android ViewPager in Kotlin
    ViewPager adds a sleek, swipeable interface that lets users effortlessly navigate between pages, just like flipping through a book. This feature is common in social media apps; for instance, WhatsApp greets you with three intuitive tabs: chats, status, and calls. This makes the app look cool. You've
    5 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