Android AdapterViewFlipper


Download AdapterViewFlipper sample

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:

  1. Android AdapterViewFlipper usage
  2. Xml attributes of Android AdapterViewFlipper
  3. Impartant methods of Android AdapterViewFlipper
  4. How to add simple animation on AdapterViewFlipper
  5. Auto/manual image slide show example

Android AdapterViewFlipper usage?


Android AdapterViewFlipper can be defined in xml layout as follows:

android:autoStart attribute of AdapterViewFlipper?


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?


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:

Impartant methods of Android AdapterViewFlipper?


Here are some impartant method of Android AdapterViewFlipper which we use frequently:

startFlipping method of AdapterViewFlipper?

As the name suggest, startFlipping method of AdapterViewFlipper used to start flipping of a AdapterViewFlipper. Syntax of using startFlipping method is given below:

stopFlipping method of AdapterViewFlipper?

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?

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?

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?

isAutoStart method of AdapterViewFlipper return true or false value depending on whether AdapterViewFlipper is auto flipping or not

Adding animation in AdapterViewFlipper?


To add simple animation on AdapterViewFlipper I created 4 xml resource under res -> anim as given below:

left_in.xml

right_out.xml

right_in.xml

left_out.xml

Last step is to call it, when user click on next, previous and play button.

Creating adapter for AdapterViewFlipper?


Here are steps to create adapter to use in AdapterViewFlipper:

  1. Define how AdapterViewFlipper screen should look like
  2. Create adapter of AdapterViewFlipper
  3. Set adapter to AdapterViewFlipper

Define how AdapterViewFlipper screen should look like?

Create an xml layout and define how you want to display AdapterViewFlipper screen.

AdapterViewFlipper screen

Create adapter of AdapterViewFlipper?

Below is ImageDetail.java, a model class which I am going to use in custom array Adapter

ImageDetail.java

Below is source of ImageArrayAdapter class.

ImageArrayAdapter.java

Set adapter to AdapterViewFlipper?

This is how we can set adapter to AdapterViewFlipper

Image slide show example using Android AdapterViewFlipper


Android 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

activity_main.xml

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:

left_in.xml

Step 6: open res -> animator and add a new file left_out.xml, replace its code with below given code:

left_out.xml

Step 7: open res -> animator and add a new file right_in.xml, replace its code with below given code:

right_in.xml

Step 8: open res -> animator and add a new file right_out.xml, replace its code with below given code:

right_out.xml

Step 9: Now create a class ImageDetail.java and replace its code with below given code:

ImageDetail.java

Step 10: Open res-> drawable and add a new xml layout named image_row.xml and replace its code with below given code:

image_row.xml

Step 11: Create a class ImageArrayAdapter.java and replace its code with below given code:

ImageArrayAdapter.java

Step 12: Open your MainActivity.java and replace it source with source of my MainActivity.java as follows:

MainActivity.java

Step 13: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.