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 stackView programmatically in kotlin in the application. We will also learn how to add stackView to linearLayout programmatically in android application.
Output

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.
<resources> <string name="app_name">DynamicStackView</string> <string name="no_image">No Image</string> </resources>
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"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/black"> <ImageView android:id="@+id/imageView" android:layout_width="260dp" android:layout_height="260dp" android:layout_margin="3dp" android:contentDescription="@string/no_image"/> </FrameLayout>
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, StackAdapter.java, in main/java/com.tutorialwing.dynamicstackview/StackAdapter.java. 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(R.id.imageView) convertView.tag = holder } else { holder = convertView.tag as ViewHolder } holder.imageView!!.setBackgroundResource(nameList[position]) 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"?> <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:background="@android:color/darker_gray" android:orientation="vertical" android:padding="20dp"> </LinearLayout>
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.support.v7.app.AppCompatActivity 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.pizza, R.drawable.pomegranate, R.drawable.strawberry, R.drawable.zespri_kiwi) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val stackView = StackView(this) stackView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) val linearLayout = findViewById<LinearLayout>(R.id.rootContainer) linearLayout?.addView(stackView) val adapter = StackAdapter(this, nameList) stackView.adapter = adapter adapter.notifyDataSetChanged() } }
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 MainActivity.java 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" 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 StackView Programmatically in Kotlin.