• Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 21 comments

SOURCE CODE [main.xml] is 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                <ListView android:id="@+id/listview"
                                android:layout_height="wrap_content" />

SOURCE CODE [ListviewOnclickExample.java] is 

package com.ListViewExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListviewOnclickExample extends Activity
                private ListView lv;
                private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                                "SEVEN", "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle icicle)

lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
lv.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
AlertDialog.Builder adb = new AlertDialog.Builder(
adb.setTitle("ListView OnClick");
adb.setMessage("Selected Item is = "
+ lv.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);

The OUTPUT will be



21 Responses so far.

  1. Darshan says:

    HOw to pass data from one lisview to another listview like if i select country i will be get state of that in listview

  2. Android says:

    Hi Dharsan,

    I think this may help you.

    package com.TwoListView;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class TwoListView extends Activity {

    String listview_array1[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };

    String listview_array2[][] = { { "11", "12" }, { "21", "22", "23" },
    { "31", "32" }, { "41", "42", "43" }, { "51", "52" } };

    ListView lv1, lv2;

    int selected = 0;

    public void onCreate(Bundle savedInstanceState) {


    lv1 = (ListView) findViewById(R.id.listView1);
    lv2 = (ListView) findViewById(R.id.listView2);

    lv1.setAdapter(new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, listview_array1));

    lv1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView arg0, View v, int position,
    long id) {
    selected = position;

    lv2.setAdapter(new ArrayAdapter(TwoListView.this,


  3. diehard says:

    after long search i get right tutorial
    great work.....

  4. abhi019 says:

    it helped grt wrk :)

  5. Thanks! I was trying OnClick handler all night and this publication help me so much, am trying to show something to verify that the OnItemClick was working but no matters because i use a TOAST to show "some text". Good, am change for yours:

    ListViewClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
    //Acciones necesarias al hacer click
    //Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
    // Toast.LENGTH_LONG);
    Object o = serverList.getItemAtPosition(position);
    AlertDialog.Builder adb = new AlertDialog.Builder(
    adb.setTitle("ListView OnClick");
    adb.setMessage("Selected Item is = "
    + o);
    adb.setPositiveButton("Ok", null);

  6. how to make a two xml files for 2listview?

  7. can you tell me how to show text after selecting an item from listview.
    for example:
    if i click on "three" then it will show me "three comes after two and before four" as scroll-able text.
    please provide full code.
    thank you in advance.

  8. Hannah says:

    Thank you so much for this. Simple and easy to understand compared to other tutorials I've found. Excellent job. :)


    I'm searching for a code ie, listview onitemclick must go to a webpage pls help me with this thanks yar

  10. Jack says:

    Hi akila krishnan,

    You may use webview on listview onitemclick. load the url you wants to display on the webview.


  11. nice thankuuuuuuuuuuuuuuuuuu

  12. How to change color of ListView items on focus and on click??

  13. Krish says:

    Hey Really Thanks man...It works perfectly.............

  14. rk says:

    i want to create an application that when i click on listview item,it dials the phone number.Thanks in advance

  15. what is android.R.Layout.simple_list_item_1? what does it contain?

  16. thankyou so much sir..i was trying to solve a program from a month i was not able to get help from anyone on my problem.Your logic gave all answers and my app is complete.

  17. Really can you solve problems practically and staff in a well explained. I wanted to ask if you can help in the following fizes as I saw the look, and I like yours and is very good, but now want to load on one and he send me to a new window.

    Thank You

  18. Admin says:

    How Do i link the Rows ONE,TWO,THREE,FOUR and FIVE with Activities
    for instance, if "ONE" is clicked , user will be taken to Another activity named Activity_one. ( i DONT want any toast to appear in my listview!)
    Thank You!

  19. Hey, it would be a great help if someone can tell how to create the PDF of an list view item on the onclick of item......

  20. hi i am creating an application in which i am using listview. when i click on listview item next page should be open.any one can help me please

  21. Saranya N says:

    Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Android Training|Android Training in chennai with placement | Android Training in velachery

Leave a Reply