Android ListView Tutorial
|
|
ListView is a ViewGroup that displays a list of scrollable items. The list items are automatically inserted into the list using a ListAdapter. This tutorial is all about creating ListView and showing a toast message on selecting single list item.
Here is our step by step tutorial-
1. Create a new project by going to File ⇒ New Android Project.
Project Name : SimpleListView
Package : com.sunil.simplelistview
Main Activity : SimpleListViewActivity.java
Build Target : Android 2.2
Build Target : Android 2.2
2. Once the project is created open your main activity (SimpleListViewActivity.java) and extend the class from ListActivity.
public class SimpleListViewActivity extends ListActivity {
3. Now we need a string resources file to store all list items. So create an XML file under res/values/ folder and name it as list_data.xml and type the following code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="social_sites">
<item>Facebook</item>
<item>Twitter</item>
<item> LinkedIn</item>
<item>MySpace</item>
<item>Google Plus+</item>
<item>Orkut</item>
<item>Pinterest</item>
<item>Meetup</item>
<item>myLife</item>
<item>Badoo</item>
</string-array>
</resources>
<resources>
<string-array name="social_sites">
<item>Facebook</item>
<item>Twitter</item>
<item> LinkedIn</item>
<item>MySpace</item>
<item>Google Plus+</item>
<item>Orkut</item>
<item>Pinterest</item>
<item>Meetup</item>
<item>myLife</item>
<item>Badoo</item>
</string-array>
</resources>
4. Now create the layout for each item that will be placed in the ListView. Create an XML file named list_item.xml and save it inside the res/layout/ folder.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
5. Open your main activity (SimpleListViewActivity.java) and type the following code.
package com.sunil.simplelistview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SimpleListViewActivity extends ListActivity {
// defining array to store string resources
String[] social_sites;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
social_sites = getResources().getStringArray(R.array.social_sites);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, social_sites));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "You have selected : "+social_sites[position], Toast.LENGTH_SHORT).show();
}
}
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SimpleListViewActivity extends ListActivity {
// defining array to store string resources
String[] social_sites;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
social_sites = getResources().getStringArray(R.array.social_sites);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, social_sites));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "You have selected : "+social_sites[position], Toast.LENGTH_SHORT).show();
}
}
6. Now run your project. You will see the listview with list of array items. When a list item is selected you will see a toast message.
That's it ! You can also download the source code of this tutorial.











NICE ONE POST MR.SUNIL
I have read your further tutorial blog also about android.I gained many things from your sites.
Post a Comment