Android ViewFlipper is subclass of ViewAnimator class. It can be used to provide smooth animation between 2 or more views that has been added to ViewFlipper. Android ViewFlipper shows one view at a time.
Android ViewFlipper can be used to flip between views automatically as well as manually.
In this example, we are going to learn following things:
Android ViewFlipper can be defined in xml layout as follows:
Here, we can add any View or ViewGroup in place of ImageView.
android:autoStart attribute of ViewFlipper define whether ViewFlipper will start flipping automatically or not. It can have true or false as its value.
android:autoStart has true value so it will flip automatically.
android:flipInterval of ViewFlipper define time in miliseconds that ViewFlipper will take to animate between views. An example of android:flipInterval attribute is given below:
Here are some impartant method of Android ViewFlipper which we use frequently:
As the name suggest, startFlipping method of ViewFlipper used to start flipping of a ViewFlipper. Syntax of using startFlipping method is given below:
As the name suggest, stopFlipping method of ViewFlipper used to stop flipping of a ViewFlipper. Syntax of using stopFlipping method is given below:
setFlipInterval method of ViewFlipper used to set flipping interval in miliseconds between views. Syntax of using setFlipInterval method is given below:
setAutoStart method of ViewFlipper handle whether auto flipping of ViewFlipper will take place or not:
Since, I passed true value in setAutoStart method of ViewFlipper, so it will start flipping automatically.
isAutoStart method of ViewFlipper return true or false value depending on whether ViewFlipper is auto flipping or not
In this example, I wanted when user fling left to right then ViewFlipper flip next image and when user fling right to left then ViewFlipper flip previous image. We need to detect touch gesture of a view so that we can implement this logic.
I created an abstract class OnFlingListener.java, as follows:
Next step is to set touch listener of ViewFlipper in our Activity class as follows:
To add simple animation on ViewFlipper I created 2 xml resource under res -> anim as given below:
Last step was to call it, when user fling on ViewFlipper left to right or right to left.
In Android ViewFlipper example, we are going to implement image slide show that can be flip automatically as well as manually.
Step 1: Create a new project in Android Studio and name it Android ViewFlipper.
Step 2: Open res -> drawable and add six images with name nature_1.jpg, nature_2.jpg, nature_3.jpg, nature_4.jpg, nature_5.jpg, nature_6.jpg
Step 3: Open res -> layout -> activity_main.xml and add following code
Step 4: Create a new directory under res and named it anim
Step 5: open res -> anim and add a new file left_to_right.xml, replace its code with below given code:
Step 6: open res -> anim and add a new file right_to_left.xml, replace its code with below given code:
Step 7: Now create an abstract class OnFlingListener.java and implement View.OnTouchListener , replace its code with below given code:
Step 8: Open your MainActivity.java and replace it source with source of my MainActivity.java as follows:
Step 9: Clean and run the project to see the result.