[Android General] Roach_:
Informations

This tutorial will be demonstrating how to build a simple android List View.

It is about creating list view and launching new activity on selecting single list item.

Below are two screenshots of the final output:

Guide

Let’s get start by creating a project in Eclipse IDE.

Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as AndroidListViewActivity.

Once the project is created open your main activity java file (in this case AndroidListViewActivity.java) and extend the class from ListActivity.

[FONT="Courier New"][B][COLOR=#006699]public class[/COLOR][/B] AndroidListViewActivity [B][COLOR=#006699]extends[/COLOR][/B] ListActivity {[/FONT]

Now we need a string resources file to store all list item labels. So create an XML file under values folder and name it as list_data.xml and paste the following code.

( Right Click on res/values ⇒ New ⇒ Android XML File)

[FONT="Courier New"][COLOR=#819f07]list_data.xml[/COLOR]
<?[B][COLOR=#006699]xml[/COLOR][/B] [COLOR="Gray"]version[/COLOR]=[COLOR="Blue"]"1.0"[/COLOR] [COLOR="Gray"]encoding[/COLOR]=[COLOR="Blue"]"utf-8"[/COLOR]?>
<[B][COLOR=#006699]resources[/COLOR][/B]>
<[B][COLOR=#006699]string-array name[/COLOR][/B]=[COLOR="Blue"]"adobe_products"[/COLOR]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe After Effects</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Bridge</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Dreamweaver</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Edge</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Fireworks</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Flash</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Photoshop</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Premiere</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Reader</[B][COLOR=#006699]item[/COLOR][/B]>
<[B][COLOR=#006699]item[/COLOR][/B]>Adobe Illustrator</[B][COLOR=#006699]item[/COLOR][/B]>
</[B][COLOR=#006699]string-array[/COLOR][/B]>
</[B][COLOR=#006699]resources[/COLOR][/B]>[/FONT]

In ListView each list item will be an xml layout, so we can customize each list item. Create an XML file under res/layout folder and name it as list_item.xml and type the following code. This xml layout will be single list item row.

( Right Click on res/layout ⇒ New ⇒ Android XML File)

[FONT="Courier New"]<?[B][COLOR=#006699]xml[/COLOR][/B] [COLOR="Gray"]version[/COLOR]=[COLOR="Blue"]"1.0"[/COLOR] [COLOR="Gray"]encoding[/COLOR]=[COLOR="Blue"]"utf-8"[/COLOR]?>
[COLOR=#819f07]<!-- Single List Item Design -->[/COLOR]
<[B][COLOR=#006699]TextView[/COLOR][/B] [COLOR="Gray"]xmlns:android[/COLOR]=[COLOR="Blue"]"http://schemas.android.com/apk/res/android"[/COLOR]
[COLOR="Gray"]android:id[/COLOR]=[COLOR="Blue"]"@+id/label"[/COLOR]
[COLOR="Gray"]android:layout_width[/COLOR]=[COLOR="Blue"]"fill_parent"[/COLOR]
[COLOR="Gray"]android:layout_height[/COLOR]=[COLOR="Blue"]"fill_parent"[/COLOR]
[COLOR="Gray"]android:padding[/COLOR]=[COLOR="Blue"]"10dip"[/COLOR]
[COLOR="Gray"]android:textSize[/COLOR]=[COLOR="Blue"]"16dip"[/COLOR]
[COLOR="Gray"]android:textStyle[/COLOR]=[COLOR="Blue"]"bold"[/COLOR] >
</[B][COLOR=#006699]TextView[/COLOR][/B]>[/FONT]

Now open your main activity java file (AndroidListViewActivity.java) and type the following code. In the following code I'm importing all XML resources data and storing them in an Array. On the next step I'm binding array to ListAdapter.

[FONT="Courier New"][COLOR=#819f07]AndroidListViewActivity.java[/color]
[B][COLOR=#006699]package[/COLOR][/B] com.androidhive.androidlistview;

[B][COLOR=#006699]import[/COLOR][/B] android.app.ListActivity;
[B][COLOR=#006699]import[/COLOR][/B] android.content.Intent;
[B][COLOR=#006699]import[/COLOR][/B] android.os.Bundle;
[B][COLOR=#006699]import[/COLOR][/B] android.view.View;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.AdapterView;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.AdapterView.OnItemClickListener;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.ArrayAdapter;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.ListView;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.TextView;

[B][COLOR=#006699]public class[/COLOR][/B] AndroidListViewActivity [B][COLOR=#006699]extends[/COLOR][/B] ListActivity {
[COLOR="Gray"]@Override[/COLOR]
[B][COLOR=#006699]public void[/COLOR][/B] onCreate(Bundle savedInstanceState) {
[B][COLOR=#006699]super[/COLOR][/B].onCreate(savedInstanceState);

[COLOR=#819f07]// storing string resources into Array[/COLOR]
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

[COLOR=#819f07]// Binding resources Array to ListAdapter[/COLOR]
[B][COLOR=#006699]this[/COLOR][/B].setListAdapter([B][COLOR=#006699]new[/COLOR][/B] ArrayAdapter<String>([B][COLOR=#006699]this[/COLOR][/B], R.layout.list_item, R.id.label, adobe_products));

}
}[/FONT]

Now run your project you can see listview with list of array items. But on clicking single list item you can see no action. So we need to start new activity on selecting single list item.

Launching new Activity on selecting single list item.

Now create new activity class under src folder. Right Click on src/package folder ⇒ New ⇒ Class and name it as SingleListItem. (SingleListItem.java)

Open your AndroidListViewActivity.java and modify the code to following. In the following code i am getting the selected list item string(product name) and sending it to new Activity.

[FONT="Courier New"][COLOR=#819f07]AndroidListViewActivity.java[/color]
[B][COLOR=#006699]package[/COLOR][/B] com.androidhive.androidlistview;

[B][COLOR=#006699]import[/COLOR][/B] android.app.ListActivity;
[B][COLOR=#006699]import[/COLOR][/B] android.content.Intent;
[B][COLOR=#006699]import[/COLOR][/B] android.os.Bundle;
[B][COLOR=#006699]import[/COLOR][/B] android.view.View;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.AdapterView;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.AdapterView.OnItemClickListener;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.ArrayAdapter;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.ListView;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.TextView;

[B][COLOR=#006699]public class[/COLOR][/B] AndroidListViewActivity [B][COLOR=#006699]extends[/COLOR][/B] ListActivity {
[COLOR="Gray"]@Override[/COLOR]
[B][COLOR=#006699]public void[/COLOR][/B] onCreate(Bundle savedInstanceState) {
[B][COLOR=#006699]super[/COLOR][/B].onCreate(savedInstanceState);

[COLOR=#819f07]// storing string resources into Array[/COLOR]
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

[COLOR=#819f07]// Binding resources Array to ListAdapter[/COLOR]
[B][COLOR=#006699]this[/COLOR][/B].setListAdapter([B][COLOR=#006699]new[/COLOR][/B] ArrayAdapter<String>([B][COLOR=#006699]this[/COLOR][/B], R.layout.list_item, R.id.label, adobe_products));

ListView lv = getListView();

[COLOR=#819f07]// listening to single list item on click[/COLOR]
lv.setOnItemClickListener([B][COLOR=#006699]new[/COLOR][/B] OnItemClickListener() {
[B][COLOR=#006699]public void[/COLOR][/B] onItemClick(AdapterView<?> parent, View view,
[B][COLOR=#006699]int[/COLOR][/B] position, [B][COLOR=#006699]long[/COLOR][/B] id) {

[COLOR=#819f07]// selected item [/COLOR]
String product = ((TextView) view).getText().toString();

[COLOR=#819f07]// Launching new Activity on selecting single List Item[/COLOR]
Intent i = [B][COLOR=#006699]new[/COLOR][/B] Intent(getApplicationContext(), SingleListItem.[B][COLOR=#006699]class[/COLOR][/B]);
[COLOR=#819f07]// sending data to new activity[/COLOR]
i.putExtra([COLOR="Blue"]"product"[/COLOR], product);
startActivity(i);

}
});
}
}[/FONT]

Now in new activity we need to display the received from listview activity.

Create a new xml file under res/layout and name it as single_list_item_view.xml and type the following code. This XML file will be layout for SingleListItem.java

[FONT="Courier New"][COLOR=#819f07]single_list_item_view.xml[/COLOR]
<?[B][COLOR=#006699]xml[/COLOR][/B] [COLOR="gray"]version[/COLOR]=[COLOR="Blue"]"1.0"[/COLOR] [COLOR="gray"]encoding[/COLOR]=[COLOR="Blue"]"utf-8"[/COLOR]?>
<[B][COLOR=#006699]LinearLayout[/COLOR][/B]
[COLOR="gray"]xmlns:android[/COLOR]=[COLOR="Blue"]"http://schemas.android.com/apk/res/android"[/COLOR]
[COLOR="gray"]android:orientation[/COLOR]=[COLOR="Blue"]"vertical"[/COLOR]
[COLOR="gray"]android:layout_width[/COLOR]=[COLOR="Blue"]"match_parent"[/COLOR]
[COLOR="gray"]android:layout_height[/COLOR]=[COLOR="Blue"]"match_parent"[/COLOR]>
<[B][COLOR=#006699]TextView[/COLOR][/B] [COLOR="Gray"]android:id[/COLOR]=[COLOR="Blue"]"@+id/product_label"[/COLOR]
[COLOR="gray"]android:layout_width[/COLOR]=[COLOR="Blue"]"fill_parent"[/COLOR]
[COLOR="gray"]android:layout_height[/COLOR]=[COLOR="Blue"]"wrap_content"[/COLOR]
[COLOR="gray"]android:textSize[/COLOR]=[COLOR="Blue"]"25dip"[/COLOR]
[COLOR="gray"]android:textStyle[/COLOR]=[COLOR="Blue"]"bold"[/COLOR]
[COLOR="gray"]android:padding[/COLOR]=[COLOR="Blue"]"10dip"[/COLOR]
[COLOR="gray"]android:textColor[/COLOR]=[COLOR="Blue"]"#ffffff"[/COLOR]/>
</[B][COLOR=#006699]LinearLayout[/COLOR][/B]>[/FONT]

Now open your second activity file i.e SingleListItem.java and paste the following code.

[FONT="Courier New"][COLOR=#819f07]SingleListItem.java[/COLOR]
[B][COLOR=#006699]package[/COLOR][/B] com.androidhive.androidlistview;

[B][COLOR=#006699]import[/COLOR][/B] android.app.Activity;
[B][COLOR=#006699]import[/COLOR][/B] android.content.Intent;
[B][COLOR=#006699]import[/COLOR][/B] android.os.Bundle;
[B][COLOR=#006699]import[/COLOR][/B] android.widget.TextView;

[B][COLOR=#006699]public class[/COLOR][/B] SingleListItem extends Activity{
[COLOR="Gray"]@Override[/COLOR]
[B][COLOR=#006699]public void[/COLOR][/B] onCreate(Bundle savedInstanceState) {
[B][COLOR=#006699]super[/COLOR][/B].onCreate(savedInstanceState);
[B][COLOR=#006699]this[/COLOR][/B].setContentView(R.layout.single_list_item_view);

TextView txtProduct = (TextView) findViewById(R.id.product_label);

Intent i = getIntent();
[COLOR=#819f07]// getting attached intent data[/COLOR]
String product = i.getStringExtra([COLOR="Blue"]"product"[/COLOR]);
[COLOR=#819f07]// displaying selected product name[/COLOR]
txtProduct.setText(product);

}
}[/FONT]

The final step is to add an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below:

[FONT="Courier New"][COLOR=#819f07]AndroidManifest.xml[/COLOR]
<?[B][COLOR=#006699]xml[/COLOR][/B] [COLOR="Gray"]version[/COLOR]=[COLOR="blue"]"1.0"[/COLOR] [COLOR="gray"]encoding[/COLOR]=[COLOR="Blue"]"utf-8"[/COLOR]?>
<[B][COLOR=#006699]manifest[/COLOR][/B] [COLOR="gray"]xmlns:android[/COLOR]=[COLOR="blue"]"http://schemas.android.com/apk/res/android"[/COLOR]
[COLOR="gray"]package[/COLOR]=[COLOR="blue"]"com.androidhive.androidlistview"[/COLOR]
[COLOR="gray"]android:versionCode[/COLOR]=[COLOR="blue"]"1"[/COLOR]
[COLOR="gray"]android:versionName[/COLOR]=[COLOR="blue"]"1.0"[/COLOR]>
<[B][COLOR=#006699]uses-sdk[/COLOR][/B] [COLOR="gray"]android:minSdkVersion[/COLOR]=[COLOR="blue"]"8"[/COLOR] />

<[B][COLOR=#006699]application[/COLOR][/B] [COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon"[/COLOR] [COLOR="gray"]android:label[/COLOR]=[COLOR="blue"]"@string/app_name"[/COLOR]>
<[B][COLOR=#006699]activity[/COLOR][/B] [COLOR="gray"]android:name[/COLOR]=[COLOR="blue"]".AndroidListViewActivity"[/COLOR]
[COLOR="gray"]android:label[/COLOR]=[COLOR="blue"]"Android List View"[/COLOR]>
<[B][COLOR=#006699]intent-filter[/COLOR][/B]>
<[B][COLOR=#006699]action[/COLOR][/B] [COLOR="gray"]android:name[/COLOR]=[COLOR="blue"]"android.intent.action.MAIN"[/COLOR] />
<[B][COLOR=#006699]category[/COLOR][/B] [COLOR="gray"]android:name[/COLOR]=[COLOR="blue"]"android.intent.category.LAUNCHER"[/COLOR] />
</[B][COLOR=#006699]intent-filter[/COLOR][/B]>
</[B][COLOR=#006699]activity[/COLOR][/B]>

<[B][COLOR=#006699]activity[/COLOR][/B] [COLOR="gray"]android:name[/COLOR]=[COLOR="blue"]".SingleListItem"[/COLOR]
[COLOR="gray"]android:label[/COLOR]=[COLOR="blue"]"Single Item Selected"[/COLOR]>
</[B][COLOR=#006699]activity[/COLOR][/B]>
</[B][COLOR=#006699]application[/COLOR][/B]>
</[B][COLOR=#006699]manifest[/COLOR][/B]>[/FONT]

Finally run your project by Right Clicking on your Project Folder ⇒ Run As ⇒ 1 Android Application.

Credits

Me - For the topic design.

And I must thank 4 other guys that helped me understand the Android and they're still teaching me:

ihavenick - The first one who helped me understand the Android!

Volk204 - He's the guy that helps me with codes and makes me understand what they do.

Tigrouzen - Lmao! Your kernels are great dude!

Rebellos - He motivated me and I thank him very much!