1

I am working on and application which has a listview , This is a custom listview with a button and textview inside it, this listview contain textview and a button . I have used Onclick listener on button so, i am unable to use onItemClick Listener on Listview , what should i do to implement that one.

Here is the code

<ListView
        android:id="@+id/lvMenuItem"
        android:layout_width="350dp"
        android:dividerHeight="3dp"
        android:layout_height="wrap_content" >
    </ListView>

LvItem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgMenu"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:clickable="false"
        android:contentDescription="@string/app_name"/>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvSubMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="" />

        <TextView
            android:id="@+id/tvType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="" />
        <TextView
        android:id="@+id/tvPrice"
        android:layout_width="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content"
        android:text="" />
    </LinearLayout>

    <Button
        android:id="@+id/btnOrder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/btnOrder" />

</LinearLayout>

OnclickListner on button is working fine what should i do for onItemCLickListner on Listview. please guide me

enter image description here

hemant
  • 564
  • 2
  • 13
  • 37

1 Answers1

1

Just make the Button non-focusable and then you will be able to recieve Button.OnClick callback as well as ListView.OnItemClick

<Button
    android:id="@+id/btnOrder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/btnOrder"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

You can find more detailed explanation here.

Community
  • 1
  • 1
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
  • Great Its Working.. Thanks. I have 1 more Doubt i have to hide two values behind the button , i have hidden 1 in SetTag how can i hide the other value? – hemant Mar 08 '13 at 12:04
  • 1
    I'm not sure I get the question, but I guess you can use `setTag(int key, Object tag)` instead of `setTag(Object tag)`. Something like `button.setTag(0, "firstValue"); button.setTag(1, "secondValue);"` ant then `button.getTag(0) ; button.getTag(1);` – Vladimir Mironov Mar 08 '13 at 12:09