h3lios
h3lios

Reputation: 104

Android set ListView items clickable when there's a ImageButton on it

I have a custom listView populated with an ArrayAdapter:

ArrayAdapter<String>  MyAdapter = new ArrayAdapter<>(
                this,
                R.layout.row,
                R.id.info,
                list
        );

My row layout is composed of a Textview and an ImageButton. I've done some research and I found that Android by default doesn't allow to click on list items that already have a focusable item on them. So, as suggested, I added the following attributes to my button layout:

android:focusable="false"
android:clickable="false"

Unfortunately, the problem persists. Does anyone knows what is the problem?

Thanks in advance.

Upvotes: 1

Views: 91

Answers (5)

akshay
akshay

Reputation: 5979

add

android:focusableInTouchMode="false"

for ImageButton

Upvotes: 1

h3lios
h3lios

Reputation: 104

Ok, I found the answer to my own question.

I need to add this line in the row layout:

  android:descendantFocusability="blocksDescendants"

With this line, I explicitly block the focusability of the child elements of the row layout. That was tricky because apparently only the imageButtons have this behavior.

Upvotes: 1

SilentKnight
SilentKnight

Reputation: 14021

Set your ImageButton like this:

android:setFocusable="false"
android:setClickable="true"

And add OnClickListener to your ImageButton and onItemClickListener to your ListView. I think it will work.

Upvotes: 0

Musa Y.
Musa Y.

Reputation: 1777

Add the following to the button attribute in your xml file:

android:focusable="false"

and remove:

android:clickable="false"

and it should work.. It'll be clickable, just won't gain focus.

However, you use android:clickable="true" for non-clickable items such as views.

Upvotes: 0

Gready
Gready

Reputation: 1164

Add this to your ImageButton objects

    android:setFocusable="false"
    android:setClickable="false"
    android.setLongClickable="false"

Upvotes: 0

Related Questions