StackView in Android Studio

Introduction

 
In this article, you will learn about StackView in Android Studio.
 

StackView

 
Stack View is a type of view that can contain images and you can show your images in a stack form and also scroll your images up and down.
 
Image of StackView
 
Clipboard04.jpg
 
Step 1
 
Create a Java class and write the following code.
 
In this first, you use all images in an ArrayList and create a stackview to hold the images. You will get all these images in a stack view by ing an object of an Adapter class that extends the Araayadapter classes. We this object to the setAdapter method and call this method with the object of the Adapter class to get all images in a stack.   
  1. import android.annotation.SuppressLint;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.*;  
  10.    
  11. import java.util.ArrayList;  
  12.    
  13. public class MainActivity extends Activity {  
  14.      StackView sv;  
  15.     @SuppressLint("NewApi")  
  16.     @Override  
  17.    
  18.     public void onCreate(Bundle savedInstanceState)  
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         StackView stk = (StackView)this.findViewById(R.id.stackView);  
  23.         ArrayList<StackItem> items = new ArrayList<StackItem>();  
  24.         items.add(new StackItem("text1"this.getResources().getDrawable(R.drawable.pic1 )));  
  25.         items.add(new StackItem("text2"this.getResources().getDrawable(R.drawable.pic2)));  
  26.         items.add(new StackItem("text3"this.getResources().getDrawable(R.drawable.pic3)));  
  27.         items.add(new StackItem("text4"this.getResources().getDrawable(R.drawable.pic4)));  
  28.         items.add(new StackItem("text5"this.getResources().getDrawable(R.drawable.pic9)));  
  29.         items.add(new StackItem("text6"this.getResources().getDrawable(R.drawable.pic5)));  
  30.         items.add(new StackItem("text7"this.getResources().getDrawable(R.drawable.pic6)));  
  31.         items.add(new StackItem("text8"this.getResources().getDrawable(R.drawable.pic7)));  
  32.         items.add(new StackItem("text9"this.getResources().getDrawable(R.drawable.pic8)));  
  33.         AdapterStack adapt = new AdapterStack(this, R.layout.second, items);  
  34.         stk.setAdapter(adapt);  
  35.     }  
  36.     public class StackItem {  
  37.         public String itemText;  
  38.         public Drawable itemPhoto;  
  39.         public StackItem(String text,Drawable photo)  
  40.         {  
  41.             this.itemPhoto = photo;  
  42.             this.itemText = text;  
  43.         }  
  44. }  
  45.     public class AdapterStack extends ArrayAdapter<StackItem> {  
  46.         private ArrayList<StackItem> items;  
  47.            private Context ctx;  
  48.         public AdapterStack(Context context, int textViewResourceId,  
  49.                             ArrayList<StackItem> objects) {  
  50.             super(context, textViewResourceId, objects);  
  51.             this.items = objects;  
  52.             this.ctx = context;  
  53.         }  
  54.         @Override  
  55.         public View getView(int position, View convertView, ViewGroup parent) {  
  56.             View v = convertView;  
  57.             if (v == null) {  
  58.                 LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  59.                 v = vi.inflate(R.layout.second, null);  
  60.             }  
  61.             StackItem m = items.get(position);  
  62.             if (m != null) {  
  63.                 TextView text = (TextView) v.findViewById(R.id.textview);  
  64.                 ImageView img = (ImageView)v.findViewById(R.id.imagteview);  
  65.                 if (text != null) {  
  66.                     text.setText(m.itemText);  
  67.                     img.setImageDrawable(m.itemPhoto);  
  68.                 }  
  69.             }  
  70.             return v;  
  71.         }  
  72.     }  
  73. }  
  74.    
Step 2
 
Create An XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.              android:layout_width="fill_parent"  
  4.              android:layout_height="fill_parent" >  
  5.         <TextView  
  6.                 android:layout_height="wrap_content"  
  7.                 android:layout_width="wrap_content"  
  8.                 android:id="@+id/textview">  
  9.         </TextView>  
  10.    
  11.         <ImageView  
  12.                 android:layout_height="wrap_content"  
  13.                 android:layout_width="wrap_content"  
  14.                 android:id="@+id/imagteview">  
  15.         </ImageView>  
  16.    
  17. </FrameLayout> 
Step 3
 
Create another XML file and write this:
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.              android:layout_width="fill_parent"  
  3.              android:layout_height="fill_parent" >  
  4.    
  5.     <StackView  
  6.             android:id="@+id/stackView"  
  7.             android:layout_width="match_parent"  
  8.             android:layout_height="match_parent"  
  9.             android:animateLayoutChanges="true"  
  10.             android:background="#F4C2C2"  
  11.    
  12.             >  
  13.    
  14.     </StackView>  
  15.    
  16. </FrameLayout> 
Step 4
 
Android Menifest.xml file.
 
In Android Menifest.xml file change the Android minsdk version because StackView does not support a minsdk version less than 11.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.stackvie"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="11"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.stackvie.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Output
 
Clipboard04.jpg
 
When you scroll down the StackView:
 
Clipboard06.jpg


Similar Articles