Create an Android StackView Programmatically in Android

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.

Output

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.

<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 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"?>
<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 will provide data to create views using stackView. So, create a java file, StackAdapter.java, 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;
	}

	@Override
	public int getCount() {
		return nameList.length;
	}

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

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	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(R.id.imageView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.imageView.setBackgroundResource(nameList[position]);
		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"?>
<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 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/MainActivity.java file and 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;

public class MainActivity extends AppCompatActivity {

	int[] nameList = {R.drawable.guava,
			R.drawable.jackfruit,
			R.drawable.mix_fruit,
			R.drawable.pizza,
			R.drawable.pomegranate,
			R.drawable.strawberry,
			R.drawable.zespri_kiwi
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

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

		LinearLayout linearLayout = findViewById(R.id.rootContainer);
		if (linearLayout != null) {
			linearLayout.addView(stackView);
		}

		StackAdapter adapter = new StackAdapter(this, nameList);
		stackView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
}

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

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

Leave a Reply