Monday, 7 April 2014

How to Create StackView in Android Application Development

This blog is describe about StackView functionality. This functionality depend upon Last in-First out or First in-Last out data structure activity.This means when we open 1st activity and then open 2nd activity, then 3rd activity, 3rd activity will in front and other 1st and 2nd activity will run in background. After click on back button 3rd activity will destroy and 2nd activity will resume. Similarly again clivk on back button 2nd activity will destroy and 1st activity will resume this functionality is also known as "Back Stack" functionality in Android.

main.xml




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stack View"
        android:layout_marginTop="20dp"
        android:textColor="#336633"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center" />

    <StackView
        android:id="@+id/stackView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </StackView>

</LinearLayout>


stack_item.xml


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

StackViewActivity.java

package com.rakesh.tiwari.google.stackview;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.StackView;
import android.widget.TextView;

public class StackViewActivity extends Activity {
StackView stkView;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StackView stkv = (StackView) findViewById(R.id.stackView1);
ArrayList<StackItem> items = new ArrayList<StackItem>();
items.add(new StackItem("text1", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text2", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text3", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text4", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text5", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text6", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text7", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text8", this.getResources().getDrawable(
R.drawable.ic_launcher)));
items.add(new StackItem("text9", this.getResources().getDrawable(
R.drawable.ic_launcher)));
StackAdapter adapt = new StackAdapter(this, R.layout.stack_item, items);
stkv.setAdapter(adapt);
}

public class StackItem {
public String itemText;
public Drawable itemPhoto;

public StackItem(String text, Drawable photo) {
this.itemPhoto = photo;
this.itemText = text;
}

}

public class StackAdapter extends ArrayAdapter<StackItem> {

private ArrayList<StackItem> items;
private Context ctx;

public StackAdapter(Context context, int textViewResourceId,
ArrayList<StackItem> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
this.ctx = context;
}

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

View v = convertView;
if (v == null) {
LayoutInflater layinfl = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layinfl.inflate(R.layout.stack_item, null);

}
StackItem sitm = items.get(position);
if (sitm != null) {
TextView textv = (TextView) v.findViewById(R.id.textView1);
ImageView imgv = (ImageView) v.findViewById(R.id.imageView1);
if (textv != null) {
textv.setText(sitm.itemText);
imgv.setImageDrawable(sitm.itemPhoto);

}

}
return v;
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.stack_view, menu);
return true;
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.google.stackview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rakesh.tiwari.google.stackview.StackViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




 StackView Functionality
 StackView Functionality

StackView App
 StackView App

BackStackView App
 BackStackView App

StackView
StackView