5

I'm working on an android app in which I'm displaying a list of items and allowing the user to click on a star icon to favourite items, which will then be displayed in a separate activity.

For this list then I am using a RelativeLayout with a ImageView for an icon, a TextView for a name of the item and an Button for the favourite button.

The problem I have is that without the button the list items touch properly and glow on response to a touch. With the button however they dont glow correctly, however they will fire any methods I place in the android:onClick xml attribute of the relativeLayout

Does anyone know of a way to fix this behaviour?

Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
Michael Allen
  • 5,712
  • 3
  • 38
  • 63

3 Answers3

8

When using an ImageButton you have to call

myImageButton.setFocusable(false);

programmatically because its constructor sets it to true. You can do so by overwriting the getView-method of your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    view.findViewById(R.id.idOfYourImageButton).setFocusable(false);
    return view;
}
Miracula
  • 81
  • 1
  • 2
1

There are a lot of stackoverflow questions on this, but none that seemed to give a simple solution, so I'm posting what worked for me here:

My specific example involves a ListFragment, each item in the listview contains a standard button. I want the button and the listview to be clickable separately.

In the listFragment, add this:

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        this.getListView().setItemsCanFocus(false);
    }

In the xml for the row item, add this to the Button XML:

android:focusable="false"

.. and that's it. Did the trick for me.

Steven Elliott
  • 3,214
  • 5
  • 29
  • 40
  • does this still allow focusing through the trackball? Would prefer a solution that supports the trackball as many old phones are still in use with them. – Michael Allen May 04 '12 at 11:01
  • Another small issue: clicking the row makes the button show it's pressed state, even though it registers `onListItemClick` on the row (and not `onClick` for the Button). Any solution for that? – Karakuri Aug 29 '12 at 21:46
1

Did you set listview focusable to be false

Here are some similar problems

Focusable EditText inside ListView

Using a checkbox with a false focusable, still prevents listview clicks

and some more here

Community
  • 1
  • 1
100rabh
  • 6,156
  • 5
  • 27
  • 41