Search code examples
androidandroid-listviewandroid-arrayadapterandroid-custom-view

Custom Listview with onItemClickListner Not working as i am using OnClickListner of Button inside listview


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


Solution

  • 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.