Amr Hesham
Amr Hesham

Reputation: 87

Android Custom ListView item unclickable

I've got a custom listView that I'm populating with this item layout. The problem is that the OnItemClickListener even handler of ListView only captures clicks on the imageView, but not on the other 2 textViews. Anyway to fix this?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/Fourdp"
    android:layout_marginLeft="@dimen/Fourdp"
    android:layout_marginRight="@dimen/Fourdp"
    android:layout_marginTop="@dimen/Fourdp"
    android:gravity="left|center"
    android:paddingBottom="5px"
    android:paddingLeft="5px"
    android:paddingTop="5px" 
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/Fourdp"
        android:layout_marginLeft="@dimen/Fourdp"
        android:layout_marginRight="@dimen/Fourdp"
        android:layout_marginTop="@dimen/Fourdp"/>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:longClickable="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/task"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#336699" />
    </LinearLayout>

</LinearLayout>

I've tried setting android:focusable="false" and android:clickable="false" to the ImageView, but still it doesn't work.

Upvotes: 1

Views: 990

Answers (3)

Padma Kumar
Padma Kumar

Reputation: 20031

//add this line in your both textview android:duplicateParentState="true" this will work.

<LinearLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:longClickable="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/task"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:duplicateParentState="true"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:duplicateParentState="true"
            android:textColor="#336699" />
    </LinearLayout>

Upvotes: 0

Greezer
Greezer

Reputation: 515

You need to implement a onclicklistner for the items in your adapter (where you inflate the row view)

public View getView(int position, View convertView, ViewGroup parent) 
        {

             View v = convertView;
             if (v == null) 
             {
                 LayoutInflater vi = (LayoutInflater)InAppPurchaseActivity.this.getParent().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 v = vi.inflate(R.layout.list_item_product, null);
             }

            // add your view with onclicklistner here


             return v;
        }

Upvotes: 0

sdabet
sdabet

Reputation: 18670

That's because the LinearLayout containing the TextViews has the android:longClickable="true" attribute.

It works if you remove it.

Why do you need it ? You can use the setOnLongClickListener() method of the ListView to detect long clicks on items.

Upvotes: 1

Related Questions