Android StackView

StackView can be defined as below –

"In Android StackView is a widget that are used to created views like stacked Cards. You can flip the front item to give space for item view just after it. Whenever you flip current item, item just behind it comes forward."

Some of the popular attributes of android StackView are –



S. No. XML Attributes Description
1 android:id This is unique id of the StackView to uniquely identify the StackView.
2 android:height Height of the StackView.
3 android:width Width of the StackView.
4 android:alpha Sets alpha to the view.
5 android:background Sets drawable to the background.
6 android:backgroundTint Sets tint to apply to the background.
7 android:clickable Specifies whether the view is clickable or not.
8 android:elevation Sets elevation of the view.
9 android:focusable Specifies whether this view can take focus or not.
10 android:visibility Specifies the visibility(VISIBLE, INVISIBLE, GONE) of the view.
11 android:animateLayoutChanges Specifies whether to run layout transition when there is any change in layout.
12 android:animationCache Specifies whether to create drawing cache for children by layout animation.
13 android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.
14 android:layoutAnimation Specifies the layout animation to be used when the viewGroup is laid out for the first time.
15 android:layoutMode Defines the layout mode of the viewGroup.
16 android:animateFirstView Defines whether to animate the current View when the ViewAnimation is first displayed.
17 android:inAnimation Identifier for the animation to use when a view is shown.
18 android:loopViews Defines whether the animator loops to the first view once it has reached the end of the list.
19 android:outAnimation Identifier for the animation to use when a view is hidden.

At first, we will create android application. Then, we will use StackView in the application.

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

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project Write application name as StackView. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 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 tutorial  to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use StackView in the application.

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

 <string name="app_name">StackView</string>
 <string name="no_image">No image</string>



Now, we will create view for single item to be used in stackView. This will be used in stackView adapter to create view for single item. So, you need to create an xml file, item.xml, in res/layout folder. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android=""

android:contentDescription="@string/no_image" />


Now, we will create adapter for stackView. So, create a java file (i.e. file) in res/main/java/com.ukacademe.stackview folder.

package com.ukacademe.stackview;

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;

In StackAdapter class, we have done following things –

  • We have defined constructor that accepts context and nameList.
  • getCount() method returns number of items in the nameList array. According number of views will be created in the stackView.
  • getItem() method returns items at that position.
  • getItemId() method returns id of the view at index position.
  • Note that we have used viewHolder pattern to create view, in getView() method, for each position in the adapter.



Since we have created view for single item and an adapter for stackView, we will use stackView in the application now.

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""



In activity_main.xml file, we have defined StackView. Now, we will access this in java file to perform some operations in it.

Open src/main/java/com.ukacademe.stackview/ file. Then, add below code into it.

package com.ukacademe.stackview;

import android.os.Bundle;
import android.widget.StackView;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

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

        StackView stackView = findViewById(;
        StackAdapter adapter = new StackAdapter(this, nameList);



We need some images in res/drawable folder to create views StackView.

In file, we have use StackView. Then, we have defined and assigned the adapter in it.

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

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

<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 below.