Create An Android AdapterViewFlipper Programmatically in Kotlin

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about how to use android adapterViewFlipper programmatically in kotlin file in the application. We will also learn how to add adapterViewFlipper to linearLayout programmatically in android application.

Output

Tutorialwing Kotlin Dynamic AdapterViewFlipper Output Android AdapterViewFlipper Programmatically in Kotlin

Tutorialwing Kotlin Dynamic AdapterViewFlipper Output

Getting Started

At first, we will create android project. Then, we will create and use adapterViewFlipper programmatically in kotlin file.

1. Creating New Project in Kotlin

Follow the steps below to create new project. Please ignore the steps if you have already created a new project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicAdapterViewFlipper. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. At this point, You will get a newly created android project successfully.

Now, we will modify xml and kotlin file to use adapterViewFlipper dynamically.

2. Modify Values Folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">DynamicAdapterViewFlipper</string>
	<string name="no_image">No Image</string>
</resources>

3. Download Drawable Resources Needed

You need some drawable images to create different views in AdapterViewFlipper. So, you can click here to download images to be used in the application.

4. Create Animations for AdapterViewFlipper

Now, we will define animations to be used whenever there is a transition between current view and another view (next/previous) in the application. So, Follow steps below to define the animations –
a. Create a folder, named as animator, in main/res folder.
b. Then, create an xml file, left_in.xml, in main/res/animator folder. Then, add below code into it.

<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"/>

c. Then, create another xml file, right_out.xml, in main/res/animator folder. Then, add below code into it.

<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"/>

5. Create View For Single Item

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/rootContainer"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<ImageView
		android:id="@+id/image"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_margin="10dp"
		android:contentDescription="@string/no_image"/>

	<TextView
		android:id="@+id/name"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textColor="@android:color/black"
		android:textSize="20sp"/>

</LinearLayout>

6. Create an Adapter For AdapterViewFlipper

Now, we will create an adapter for adapterViewFlipper that are responsible to provide data and create view in adapterViewFlipper. So, Create a kotlin file, CustomAdapter.kt, in main/java/com.tutorialwing.dynamicadapterviewflipper folder. Then, add below code into it.

package com.tutorialwing.dynamicadapterviewflipper

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

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 ?: 0
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView: View = inflater.inflate(R.layout.item, parent, false)
        val nameView = convertView.findViewById<TextView>(R.id.name)
        val imageView = convertView.findViewById<ImageView>(R.id.image)
        nameView.text = nameList!![position]
        imageView.setImageResource(imageList[position])
        return convertView
    }
}

Adapter is responsible to provide data and create view for adapterViewFlipper. getView() method is responsible to create view for a given position in adapter.

7. Modify Layout Folder

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/rootContainer"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical">
</LinearLayout>

In activity_main.xml file, we have defined linearLayout, with id rootContainer, that will act as container for the adapterViewFlipper created programmatically in the application. Now, we will create adapterViewFlipper programmatically in kotlin file.

8. Create Android AdapterViewFlipper Programmatically / Dynamically in Kotlin

Open app/src/main/java/com.tutorialwing.dynamicadapterviewflipper/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.dynamicadapterviewflipper

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import android.widget.AdapterViewFlipper
import android.widget.LinearLayout

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val adapterViewFlipper = AdapterViewFlipper(this, null)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        adapterViewFlipper.layoutParams = layoutParams

        adapterViewFlipper.setInAnimation(applicationContext, R.animator.left_in)
        adapterViewFlipper.setOutAnimation(applicationContext, R.animator.right_out)

        adapterViewFlipper.flipInterval = 3200
        adapterViewFlipper.isAutoStart = true

        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

        val linearLayout = findViewById<LinearLayout>(R.id.rootContainer)
        linearLayout?.addView(adapterViewFlipper)
    }
}

here, we have created adapterViewFlipper programmatically in kotlin file ( In MainActivity.kt file) in the application. Then, we have set layout params, in and out animations, flip intervals etc. in it. Then, we have created adapter and set it in AdapterViewFlipper. At last, we have added this adapterViewFlipper in linearLayout.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file is –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamicadapterviewflipper"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Creating Android AdapterViewFlipper Programmatically in Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners