How to example create list view layout in Android


Today I will show you how to create simple list view in Android. I assume you already have “Hello World” projects. FYI, there are several Layout Views type in Android like :
1. Linear Layout
2. Relative Layout
3. Table Layout
4. Grid View
5. Tab Layout
6. List View

For futhermore explanation of this Layout, you go here. Now, we will learn about how to use List View.

First, open your activity files, eg : HelloWorldActivity.java (helloworld/src/helloworld.namespace/HelloWorldActivity.java).

Now we create simple List View in Android :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package helloworld.android.namespace;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;

public class YahooActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
       
    }
   
    protected ListAdapter createAdapter() {
        String[] testValues = new String[] {
                "Hello 1",
                "Hello 2"
        };
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout
                                    .simple_list_item_1, testValues);
       
        return adapter;
    }    
}

Running this source will show List View. Then we will try to add trigger action if list clicked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class YahooActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
       
        ListView lv  = getListView();
        lv.setTextFilterEnabled(true);
       
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
               
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
   
    protected ListAdapter createAdapter() {
        String[] testValues = new String[] {
                "Hello 1",
                "Hello 2"
        };
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout
                                    .simple_list_item_1, testValues);
       
        return adapter;
    }    
}

Running this source and try click the list. Now you have simple ListView application in Android.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.