Create An Android StackView Programmatically in Kotlin

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


Tutorialwing Kotlin Dynamic StackView Output Android StackView Programmatically in kotlin

Tutorialwing Kotlin Dynamic StackView Output

Getting Started

At first, we will create android project. Then, we will create and use stackView 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 DynamicStackView. 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 stackView dynamically.

2. Modify Values Folder

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

	<string name="app_name">DynamicStackView</string>
	<string name="no_image">No Image</string>

3. Create View For Single Item

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

<?xml version="1.0" encoding="utf-8"?>



4. Create Adapter For StackView

Now, we will create adapter for stackView that are responsible to provide data in stackView. So, create a new file,, in main/java/com.tutorialwing.dynamicstackview/ Then, add below code into it.

package com.tutorialwing.dynamicstackview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView

class StackAdapter internal constructor(context: Context, private val nameList: 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, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val holder: ViewHolder
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item, parent, false)
            holder = ViewHolder()
            holder.imageView = convertView!!.findViewById(
            convertView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
        return convertView

    inner class ViewHolder {
        internal var imageView: ImageView? = null

Adapter provides data in the stackView. We have used viewHolder pattern to create views in the stackView.

5. Modify Layout Folder

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

<?xml version="1.0" encoding="utf-8"?>


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

6. Create Android StackView Programmatically / Dynamically in Kotlin

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

package com.tutorialwing.dynamicstackview

import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.StackView

class MainActivity : AppCompatActivity() {

    private var nameList = intArrayOf(R.drawable.guava, R.drawable.jackfruit, R.drawable.mix_fruit,, R.drawable.pomegranate, R.drawable.strawberry, R.drawable.zespri_kiwi)

    override fun onCreate(savedInstanceState: Bundle?) {

        val stackView = StackView(this)
        stackView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

        val linearLayout = findViewById<LinearLayout>(

        val adapter = StackAdapter(this, nameList)
        stackView.adapter = adapter

You will need drawable images stored in res/drawable folder in the application. So, you can click here to download images to be used in the application.

In file, we have created StackView widget. Then, we have set layout params in it. After that, we have added this stackView widget in the linearLayout, having id rootContainer. Then, we have created adapter for stackView and set it in the view.

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.dynamicstackview"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


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

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