Create an Android StackView Programmatically in Android

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

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


Tutorialwing Android Dynamic StackView Output Android StackView Programmatically in Android

Tutorialwing Android Dynamic StackView Output

Getting Started

At first, we will create an android application. Then, we will use stackView widget in the application.

1. Creating New Project

Follow the steps below to create a 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, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use android stackView programmatically.

2. Modify values folder

Open res/values/strings.xml file and 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 in stackView. So, create a new xml file, item.xml, in res/layout folder. Then, we 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 will provide data to create views using stackView. So, create a java file,, in main/java/com.tutorialwing.dynamicstackview folder. 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;

public class StackAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private int[] nameList;

	StackAdapter(Context context, int[] nameList) {
		this.inflater = LayoutInflater.from(context);
		this.nameList = nameList;

	public int getCount() {
		return nameList.length;

	public Object getItem(int position) {
		return nameList[position];

	public long getItemId(int position) {
		return position;

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item, parent, false);
			holder = new ViewHolder();
			holder.imageView = convertView.findViewById(;
		} else {
			holder = (ViewHolder) convertView.getTag();
		return convertView;

	public class ViewHolder {
		ImageView imageView;

Here, we are providing context and nameList in the constructor, StackAdapter(…, …) method. Based on the data available in the nameList, different views in stackView will be created.

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 linerLayout, with id rootContainer, that will act as container for the stackView widget created programmatically in the application.

6. Create Android StackView Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicstackview/ file and 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;

public class MainActivity extends AppCompatActivity {

	int[] nameList = {R.drawable.guava,

	protected void onCreate(Bundle savedInstanceState) {

		StackView stackView = new StackView(this);
		stackView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

		LinearLayout linearLayout = findViewById(;
		if (linearLayout != null) {

		StackAdapter adapter = new StackAdapter(this, nameList);

We need some images stored in drawable folder that are used to create views for stackView. So, you can click here to download images to be used in the application.

In file, we have created android stackView programmatically. Then, we have set layout params in it. After that, we have added stackView in linearLayout. Then, we have created an adapter and set it into stackView. based on the data provided in the adapter, different views will be created.

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

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

<?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 application, we will get output as shown above.

That’s the end of tutorial on Creating Android StackView Programmatically.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at 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.
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