Android StackView
Android StackView
- StackView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<RelativeLayout 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" >
<StackView
android:id="@+id/stackView1"
android:animateLayoutChanges="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</StackView>
</RelativeLayout>
Step 3 : Open res -> layout -> item.xml and add following code :
<RelativeLayout 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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.StackView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StackView stk = (StackView)this.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)));
StackAdapter adapt = new StackAdapter(this, R.layout.item, items);
stk.setAdapter(adapt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 5 : Open src -> package -> StackItem.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.graphics.drawable.Drawable;
public class StackItem {
public String text;
public Drawable img;
public StackItem(String text,Drawable photo)
{
this.img = photo;
this.text = text;
}
}
Step 6 : Open src -> package -> StackAdapter.java and add following code :
package com.gudivada.hemanthsomaraju;
import java.util.ArrayList;
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 android.widget.TextView;
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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item, null);
}
StackItem m = items.get(position);
if (m != null) {
TextView text = (TextView) v.findViewById(R.id.textView1);
ImageView img = (ImageView)v.findViewById(R.id.imageView1);
if (text != null) {
text.setText(m.text);
img.setImageDrawable(m.img);
}
}
return v;
}
}
Step 7 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 8 : Our output will be like this :