Android AdapterViewFlipper Using Kotlin With Example

In this article, we will learn about android AdapterViewFlipper using Kotlin. We will go through various example that demonstrates how to use different attributes of AdapterViewFlipper. For example,

In this article, we will get answer to questions like –

  • What is AdapterViewFlipper?
  • Why should we consider AdapterViewFlipper while designing ui for any app?
  • What are possibilities using AdapterViewFlipper while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin AdapterViewFlipper Output Android AdapterViewFlipper Using Kotlin With Example

Tutorialwing Kotlin AdapterViewFlipper Output

Getting Started

We can define android AdapterViewFlipper widget as below –

AdapterViewFlipper is subclass of ViewFlipper that are used to switch between views using some animations. Only one view is shown at a time.

Now, how do we use AdapterViewFlipper in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as AdapterViewFlipper. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android AdapterViewFlipper Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using AdapterViewFlipper in Kotlin

Follow steps below to use AdapterViewFlipper in newly created project –

  • Download Drawable Resources Needed

    We need some drawable images to create views by AdapterViewFlipper. So, we can click here to download images to be used in the application.

  • Create Animation For AdapterViewFlipper

    Now, we are going to define the animations that will be used whenever current view is replaced by next/previous view.
    So, Follow steps below to define animations –

    1. Create a folder, named as animator, in main/res folder.
    2. Then, create an xml file, named as left_in.xml, in main/res/animator folder. Then, add below code into it ( in main/res/animator folder).
      <objectAnimator
      	xmlns:android="http://schemas.android.com/apk/res/android"
      	android:duration="600"
      	android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      	android:propertyName="x"
      	android:valueFrom="-1500"
      	android:valueTo="0"
      	android:valueType="floatType"/>
      
    3. Then, create an xml file, named as right_out.xml, in main/res/animator folder. Then, add below code into it (in main/res/animator folder).
      <objectAnimator
      	xmlns:android="http://schemas.android.com/apk/res/android"
      	android:duration="600"
      	android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      	android:propertyName="x"
      	android:valueFrom="0"
      	android:valueTo="1500"
      	android:valueType="floatType"/>
      
  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">AdapterViewFlipper</string>
        <string name="no_image">No Image</string>
    </resources>
    
  • Create View For Single Item

    Now, we will create view for single item in adapterViewFlipper. So, create an xml file, item.xml file, in res/layout folder. Then, add below code into it.

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:contentDescription="@string/no_image"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • Create Adapter For AdapterViewFlipper

    Now, we will create adapter for adapterViewFlipper that are responsible to create different views in AdapterViewFlipper. So, Create a kotlin file, CustomAdapter.kt file, in main/java/com.tutorialwing.adapterflipper folder. Then, add below code into it( in main/java/com.tutorialwing.adapterviewflipper/CustomAdapter.kt file)

    package com.tutorialwing.adapterviewflipper
    
    import android.content.Context
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.BaseAdapter
    import android.widget.ImageView
    import android.widget.TextView
    import com.tutorialwing.adapterviewflipper.databinding.ItemBinding
    
    class CustomAdapter internal constructor(
    	context: Context,
    	private val nameList: Array<String>,
    	private val imageList: IntArray
    ) : BaseAdapter() {
    
    	private val inflater: LayoutInflater = LayoutInflater.from(context)
    
    	override fun getCount(): Int {
    		return nameList.size
    	}
    
    	override fun getItem(position: Int): Any {
    		return nameList[position]
    	}
    
    	override fun getItemId(position: Int): Long {
    		return position.toLong()
    	}
    
    	override fun getView(position: Int, view: View?, parent: ViewGroup): View {
    		var convertView = view
    		val holder: ViewHolder
    		if (convertView == null) {
    			val binding = ItemBinding.inflate(inflater)
    			convertView = binding.root
    			holder = ViewHolder()
    			holder.imageView = binding.image
    			holder.name = binding.name
    			convertView.tag = holder
    		} else {
    			holder = convertView.tag as ViewHolder
    		}
    		holder.name!!.text = nameList[position]
    		holder.imageView!!.setBackgroundResource(imageList[position])
    		return convertView
    	}
    
    	inner class ViewHolder {
    		internal var imageView: ImageView? = null
    		internal var name: TextView? = null
    	}
    }
    

    In CustomAdapter.kt, getView() method is responsible to create different views for given position in adapter in AdapterViewFlipper.

  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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"
        tools:context=".MainActivity">
    
        <AdapterViewFlipper
            android:id="@+id/adapterViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:autoStart="true"
            android:flipInterval="3200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In activity_main.xml file, we have defined AdapterViewFlipper widget. android:autoStart=”” is responsible to specify whether view in adapterViewFlipper will start flipping automatically or not. android:flipInterval=”” defines the interval after which current view will be switched by next/previous view.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.adapterviewflipper
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.adapterviewflipper.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupAdapterViewFlipper()
    	}
    
    	private fun setupAdapterViewFlipper() {
    		val adapterViewFlipper = binding.adapterViewFlipper
    		adapterViewFlipper.setInAnimation(applicationContext, R.animator.left_in)
    		adapterViewFlipper.setOutAnimation(applicationContext, R.animator.right_out)
    
    		val nameList = arrayOf(
    			"Guava",
    			"JackFruit",
    			"Mix Fruit",
    			"Apple",
    			"Pomegranate",
    			"Strawberry",
    			"Zespri Kiwi"
    		)
    		val imageList = intArrayOf(
    			R.drawable.guava,
    			R.drawable.jackfruit,
    			R.drawable.mix_fruit,
    			R.drawable.apple,
    			R.drawable.pomegranate,
    			R.drawable.strawberry,
    			R.drawable.zespri_kiwi
    		)
    
    		val adapter = CustomAdapter(this, nameList, imageList)
    		adapterViewFlipper.adapter = adapter
    	}
    }
    

    We have accessed AdapterViewFlipper using kotlin file (MainActivity.kt file) in the application. Then, we have set in and out animations in it that are used whenever current view is replaced by next/previous view. After that, we have created an adapter and set it into adapterViewFlipper using kotlin file (MainActivity file).

Now, run the application. We will get output as below –

Tutorialwing Kotlin AdapterViewFlipper Output Android AdapterViewFlipper Using Kotlin With Example

Tutorialwing Kotlin AdapterViewFlipper Output

Different Attributes of AdapterViewFlipper in XML

Now, we will see how to use different attributes of Android AdapterViewFlipper using Kotlin to customise it –

Set Id of AdapterViewFlipper

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of AdapterViewFlipper using android:id attribute like below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        />

Here, we have set id of AdapterViewFlipper as adapterViewFlipper_ID using android:id=”” attribute. So, if we need to reference this AdapterViewFlipper, we need to use this id – adapterViewFlipper_ID.
Learn to Set ID of AdapterViewFlipper Dynamically

Set Width of AdapterViewFlipper

We use android:layout_width=”” attribute to set width of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of AdapterViewFlipper Dynamically

Set Height of AdapterViewFlipper

We use android:layout_height=”” attribute to set height of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of AdapterViewFlipper Dynamically

Set Padding of AdapterViewFlipper

We use android:padding=”” attribute to set padding of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in AdapterViewFlipper using android:padding=”” attribute.
Learn to Set Padding of AdapterViewFlipper Dynamically

Set Margin of AdapterViewFlipper

We use android:layout_margin=”” attribute to set margin of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in AdapterViewFlipper using android:layout_margin=”” attribute.
Learn to Set Margin of AdapterViewFlipper Dynamically

Set Background of AdapterViewFlipper

We use android:background=”” attribute to set background of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in AdapterViewFlipper using android:background=”” attribute.
Learn to Set Background of AdapterViewFlipper Dynamically

Set Visibility of AdapterViewFlipper

We use android:visibility=”” attribute to set visibility of AdapterViewFlipper.
We can do it as below –

    <AdapterViewFlipper
        android:id="@+id/adapterViewFlipper_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of AdapterViewFlipper using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of AdapterViewFlipper Dynamically

Till now, we have see how to use android AdapterViewFlipper using Kotlin. We have also gone through different attributes of AdapterViewFlipper to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android AdapterViewFlipper Widget

Below are the various attributes that are used to customise android AdapterViewFlipper Widget. However, you can check the complete list of attributes of AdapterViewFlipper in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of android adapterViewFlipper widget are –

Sr. XML Attributes Description
1 android:autoStart If set true, it will automatically start animating views
2 android:flipInterval Specifies duration after which current view will be replaced by next/previous view

Some of the popular attributes of android AdapterViewFlipper inherited from AdapterViewAnimator are –

Sr. XML Attributes Description
1 android:animateFirstView Specifies whether to animate first view when viewAnimation is first displayed
2 android:inAnimation Specifies the animation to use when view is shown
3 android:loopViews Specifies whether to start from first view if the animator has at end of the list
4 android:outAnimation Specifies the animation to use when view is hidden

Some of the popular attributes of android AdapterViewFlipper inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animateLayoutChanges Specifies whether to run layoutTransition when there is any changes in layout
2 android:animationCache Specifies whether layout animations should create a drawing cache for their children
3 android:clipToPadding Specifies whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero
4 android:layoutAnimation Specifies the layout animation to use the first time the ViewGroup is laid out
5 android:layoutMode Specifies the layout mode of the viewGroup

Some of the popular attributes of android AdapterViewFlipper inherited from View are –

Sr. XML Attributes Description
1 android:alpha Specifies alpha of the view
2 android:background Specifies background of the view
3 android:backgroundTint Specifies tint to apply to the background
4 android:backgroundTintMode Specifies blending mode to apply to tint of the background
5 android:clickable Specifies whether this view is clickable or not
6 android:elevation Specifies z-depth of the view
7 android:focusable Specifies whether this view can take focus or not
8 android:foreground Specifies drawable to be shown above the view

We have seen different attributes of AdapterViewFlipper and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is AdapterViewFlipper, how can we use android AdapterViewFlipper using Kotlin ? etc. We also went through different attributes of android AdapterViewFlipper.

Leave a Reply

We are actively looking for Paid Content Writer. Email us at tutorialwing@gmail.com if you are interested.