The Wayback Machine - https://web.archive.org/web/20251224112911/https://www.geeksforgeeks.org/android/adapterviewflipper-in-android-with-example/
  • Year-End SaleExplore
  • AdapterViewFlipper in Android with Example

    Last Updated : 15 Jul, 2025

    The AdapterViewFlipper class is a subclass of the ViewAnimator class and is used to flip between 2 or more views, such that only one view is displayed at a time. It is commonly used in slides. It is an element of the transition widget which helps to add transitions on the views. It is mainly useful to animate a view on the screen. AdapterViewFlipper switches smoothly between two or more views (TextView, ImageView, or any Layout) and thus provides a way of transitioning from one view to another through appropriate animations.

    Below is a preview sample of AdapterViewFlipper.


    Difference Between ViewFlipper Vs AdapterViewFlipper

    ViewFlipper and AdapterViewFlipper both are subclasses of ViewAnimator. The ViewFlipper is initially used to display all slide views fixed. This means that views will not be recycled. AdapterViewFlipper uses an Adapter to fill data (similar to ListView/Spinner/RecyclerView etc), so the children are determined on the fly and the views representing the children can be recycled. So AdapterViewFlipper is used to display all child views. So there is room for recycling views and loading views dynamically.

    Steps for Creating AdapterViewFlipper

    Step 1: Create a New Project

    To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

    Note: that select Java/Kotlin as the programming language.

    Step 2: Working with activity_main.xml file

    Click on app > res > layout > activity_main.xml and add a TextView to display a text and the AdapterViewFlipper to display the functionality. Below is the complete code for the activity_main.xml file. 

    activity_main.xml:

    XML
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
        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"
        android:background="@color/white"
        tools:context=".MainActivity">
    
        <!--Text view to display Global stats-->
        <TextView
            android:id="@+id/heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="Gallery"
            android:textAlignment="center"
            android:textSize="28sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <!--AdapterViewFlipper to display the functionality-->
        <AdapterViewFlipper
            android:id="@+id/adapterViewFlipper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/heading" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Layout:

    Layout


    Step 3: Create another Layout file

    Now create another XML layouts file by right-clicking on res -> layout -> new -> Layout Resource File and name it as list_item.xml. In this file add an ImageView and TextView to use it in the Adapter. Below is the complete code for the list_item.xml file.  

    list_item.xml:

    XML
    <?xml version="1.0" encoding="utf-8"?>
    <!--Constraint Layout to display all the details-->
    <androidx.constraintlayout.widget.ConstraintLayout 
        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="wrap_content">
    
        <!--Image view to display-->
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <!--Text view to display stats coordinate with image-->
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Example Text"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Layout:

    Layout_2


    Step 4: Working with MainActivity file

    Open MainActivity and add the below code to initiate the AdapterViewFlipper. Firstly create two arrays one for images and the other for names. After creating, set the adapter to fill the data in the view. Finally set the auto start and flip interval time so that AdapterViewFlipper switch between the views and the current view will go out and the next view will come in after the given time interval. Below is the complete code for the MainActivity file.

    MainActivity File:

    Java
    package org.geeksforgeeks.demo;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterViewFlipper;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        private AdapterViewFlipper adapterViewFlipper;
    
        private final int[] images = {
            R.drawable.k1,
            R.drawable.k2,
            R.drawable.k3,
            R.drawable.k4
        };
    
        private final String[] names = {
            "Top of the mountains",
            "Holding a flower with an ocean view",
            "A beautiful sky",
            "A tower"
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Link objects with their respective IDs from the XML layout
            adapterViewFlipper = findViewById(R.id.adapterViewFlipper);
            CustomAdapter customAdapter = new CustomAdapter(this, names, images);
            adapterViewFlipper.setAdapter(customAdapter);
            adapterViewFlipper.setFlipInterval(2600);
            adapterViewFlipper.setAutoStart(true);
        }
    
        public static class CustomAdapter extends BaseAdapter {
            private final Context context;
            private final String[] names;
            private final int[] images;
            private final LayoutInflater inflater;
    
            public CustomAdapter(Context context, String[] names, int[] images) {
                this.context = context;
                this.names = names;
                this.images = images;
                this.inflater = LayoutInflater.from(context);
            }
    
            @Override
            public int getCount() {
                return names.length;
            }
    
            @Override
            public Object getItem(int position) {
                return names[position];
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView != null ? convertView : inflater.inflate(R.layout.list_item, parent, false);
    
                // Link views with their IDs in the XML layout
                TextView nameTextView = view.findViewById(R.id.name);
                ImageView imageView = view.findViewById(R.id.image);
    
                // Set data in views
                nameTextView.setText(names[position]);
                imageView.setImageResource(images[position]);
    
                return view;
            }
        }
    }
    
    Kotlin
    package org.geeksforgeeks.demo
    
    import android.content.Context
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.AdapterViewFlipper
    import android.widget.BaseAdapter
    import android.widget.ImageView
    import android.widget.TextView
    import androidx.appcompat.app.AppCompatActivity
    
    class MainActivity : AppCompatActivity() {
    
        private lateinit var adapterViewFlipper: AdapterViewFlipper
    
        private val images = intArrayOf(
            R.drawable.k1,
            R.drawable.k2,
            R.drawable.k3,
            R.drawable.k4
        )
    
        private val names = arrayOf(
            "Top of the mountains",
            "Holding a flower with a ocean view",
            "A beautiful sky",
            "A tower"
        )
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // Link objects with their respective IDs from the XML layout
            adapterViewFlipper = findViewById(R.id.adapterViewFlipper)
            val customAdapter = CustomAdapter(this, names, images)
            adapterViewFlipper.adapter = customAdapter
            adapterViewFlipper.flipInterval = 2600
            adapterViewFlipper.isAutoStart = true
        }
    
        class CustomAdapter(
            private val context: Context,
            private val names: Array<String>,
            private val images: IntArray
        ) : BaseAdapter() {
    
            private val inflater: LayoutInflater = LayoutInflater.from(context)
    
            override fun getCount(): Int = names.size
    
            override fun getItem(position: Int): Any = names[position]
    
            override fun getItemId(position: Int): Long = position.toLong()
    
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = convertView ?: inflater.inflate(R.layout.list_item, parent, false)
    
                // Link views with their IDs in the XML layout
                val nameTextView: TextView = view.findViewById(R.id.name)
                val imageView: ImageView = view.findViewById(R.id.image)
    
                // Set data in views
                nameTextView.text = names[position]
                imageView.setImageResource(images[position])
    
                return view
            }
        }
    }
    

    Output:

    Now connect your device with a USB cable or an Emulator and launch the application. You will see an Adaptive flipping of the image which will keep switching within a short period of time. 


    Comment