StackView in Android

StackView is a collection of card view , where the font item can be flipped to give room for the item after it . For more information about the StackView click here
          Follow the bellow steps to create a StackView in Android .

Step 1: Create Project 
          a) Open Android Studio
          b) Go to File >New> New Project  > Project Name  > Next > Next > Next > Finish 

Step 2 : Layout Design 
             open activity_main.xml file and add the bellow code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <StackView
        android:id="@+id/sv"
        android:animateLayoutChanges="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </StackView>
</RelativeLayout>

Step 3: Create a Layout file "stack_items.xml" for showing each row in 
             StackView and add the bellow code .  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
         />
</LinearLayout>

Step 4: Model Class 
          Create a java class "StackListItem.java" and add the bellow code.
import android.graphics.drawable.Drawable;

/**
 * Created by anupam on 14/10/16.
 */
public class StackListItem {
    Drawable image;

    public Drawable getImage() {
        return image;
    }

    public void setImage(Drawable image) {
        this.image = image;
    }
}


Step 5: Adapter Class 
          Create a java class "AdapterStackList.java" and add the bellow code.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.util.List;


/**
 * Created by anupam on 14/10/16.
 */
public class AdapterStackList extends ArrayAdapter<StackListItem> {

    Context context;
    List<StackListItem> list;

    public AdapterStackList(Context context, int resource, List<StackListItem> objects) {
        super(context, resource, objects);
        this.context=context;
        list=objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view=convertView;
        if(view==null){
            LayoutInflater vi = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.stack_items, null);
        }

        StackListItem item=list.get(position);

        if(item!=null){
            ImageView imageView=(ImageView)view.findViewById(R.id.iv);
            imageView.setImageDrawable(item.getImage());
        }

        return view;
    }
}


Step 6 : Code 
             open MainActivity class and add the bellow code

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.StackView;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
    StackView stackView;
    Context mContext;
    ArrayList<StackListItem> listItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        stackView=(StackView)findViewById(R.id.sv);
        listItems=new ArrayList<StackListItem>();

        StackListItem item=new StackListItem();
        item.setImage(this.getResources().getDrawable(R.drawable.a));
        listItems.add(item);

        StackListItem item1=new StackListItem();
        item1.setImage(this.getResources().getDrawable(R.drawable.b));
        listItems.add(item1);

        StackListItem item2=new StackListItem();
        item2.setImage(this.getResources().getDrawable(R.drawable.c));
        listItems.add(item2);

        StackListItem item3=new StackListItem();
        item3.setImage(this.getResources().getDrawable(R.drawable.d));
        listItems.add(item3);

        StackListItem item4=new StackListItem();
        item4.setImage(this.getResources().getDrawable(R.drawable.e));
        listItems.add(item4);
        AdapterStackList adapterStackList=new AdapterStackList(this,R.layout.stack_items,listItems);

        stackView.setAdapter(adapterStackList);

    }
}
Find Us : 
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial
Output : 

                 


                

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Coordinator Layout in Android