VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-add-viewflipper-in-android/

⇱ How to add ViewFlipper in android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to add ViewFlipper in android?

Last Updated : 18 Feb, 2021
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.
  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.
  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.
  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.
Output:
Add autoStart tag in ViewFlipper and set its value to true. Then it will work like this.
Comment
Article Tags:
Article Tags:

Explore