Android AdapterViewFlipper is subclass of ViewAnimator class like AdapterViewFlipper. It can be used to provide smooth animation between 2 or more views that has been added to AdapterViewFlipper. Android AdapterViewFlipper shows one view at a time.
Android AdapterViewFlipper can be used to flip between views automatically as well as manually.
In AdapterViewFlipper is very similar to ViewFlipper with only difference that we can use Adapter to add views in AdapterViewFlipper which is not possible with ViewFlipper.
In this example, we are going to learn following things:
Android AdapterViewFlipper can be defined in xml layout as follows:
android:autoStart attribute of AdapterViewFlipper define whether AdapterViewFlipper 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 AdapterViewFlipper define time in miliseconds that AdapterViewFlipper will take to animate between views. An example of android:flipInterval attribute is given below:
Here are some impartant method of Android AdapterViewFlipper which we use frequently:
As the name suggest, startFlipping method of AdapterViewFlipper used to start flipping of a AdapterViewFlipper. Syntax of using startFlipping method is given below:
As the name suggest, stopFlipping method of AdapterViewFlipper used to stop flipping of a ViewFlipper. Syntax of using stopFlipping method is given below:
setFlipInterval method of AdapterViewFlipper used to set flipping interval in miliseconds between views. Syntax of using setFlipInterval method is given below:
setAutoStart method of AdapterViewFlipper handle whether auto flipping of AdapterViewFlipper will take place or not:
Since, I passed true value in setAutoStart method of AdapterViewFlipper, so it will start flipping automatically.
isAutoStart method of AdapterViewFlipper return true or false value depending on whether AdapterViewFlipper is auto flipping or not
To add simple animation on AdapterViewFlipper I created 4 xml resource under res -> anim as given below:
Last step is to call it, when user click on next, previous and play button.
Here are steps to create adapter to use in AdapterViewFlipper:
Create an xml layout and define how you want to display AdapterViewFlipper screen.
Below is ImageDetail.java, a model class which I am going to use in custom array Adapter
Below is source of ImageArrayAdapter class.
This is how we can set adapter to AdapterViewFlipper
In Android AdapterViewFlipper 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 AdapterViewFlipper.
Step 2: Open res -> drawable and add six images with name taj_1.jpg, taj_2.jpg, taj_3.jpg, taj_4.jpg, taj_5.jpg, taj_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 animator
Step 5: open res -> animator and add a new file left_in.xml, replace its code with below given code:
Step 6: open res -> animator and add a new file left_out.xml, replace its code with below given code:
Step 7: open res -> animator and add a new file right_in.xml, replace its code with below given code:
Step 8: open res -> animator and add a new file right_out.xml, replace its code with below given code:
Step 9: Now create a class ImageDetail.java and replace its code with below given code:
Step 10: Open res-> drawable and add a new xml layout named image_row.xml and replace its code with below given code:
Step 11: Create a class ImageArrayAdapter.java and replace its code with below given code:
Step 12: Open your MainActivity.java and replace it source with source of my MainActivity.java as follows:
Step 13: Clean and run the project to see the result.