1

ListView:

<ListView android:id="@+id/list11" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:layout_weight="1" 
          android:cacheColorHint="#00000000"> 
</ListView>

custome.xml:

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

    <TextView
        android:id="@+id/txt_groupname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_weight="3"
        android:text="asdasd"
        android:textColor="@color/TextColor" />

    <ImageButton
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/delete_gp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

Activity.java:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long itemValue = listView.getItemIdAtPosition(position);
        Log.e("==>", "" + itemValue);
    }

});

When I click on the <ImageButton> it works fine, but when I click on <ListView> it does not work.

How can I solve this problem?

10
  • My Listview is :- <ListView android:id="@+id/list11" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:cacheColorHint="#00000000" > </ListView> Commented Apr 2, 2014 at 11:38
  • show your baseadapter. Commented Apr 2, 2014 at 11:42
  • @Go2 try android:descendantFocusability="blocksDescendants" for the root elementin custome.xml Commented Apr 2, 2014 at 11:43
  • @Raghunandan does it require if we had kept all child as android:focusable="false" .? :-/ Commented Apr 2, 2014 at 11:44
  • @SilentKiller try it yourself with ImageButton Commented Apr 2, 2014 at 11:45

3 Answers 3

2

ImageButton takes focus when you click on list item

Add the below

android:descendantFocusability="blocksDescendants" 

to the root element in custome.xml

Sign up to request clarification or add additional context in comments.

2 Comments

@Go2 also what does this return listView.getItemIdAtPosition(position). You need to cast it to the proper type. What is the datatype?
@Go2 cast it to long its better
0

You will use this code and it will ask to cast the object you just put the casting after it will fine....

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long itemValue = parent.getItemIdAtPosition(position);
        Log.e("==>", "" + itemValue);
    }

});

Comments

0

add android:clickable="true" in the textview and the imagebutton also

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.