1

I have a ListActivity; and for each item in the ListView there is a checkbox.

When you touch a list item, another Activity launches.

When you use the trackpad/trackball to highlight (read: select) an item and click the trackpad, it essentially simulates touching the item. This causes my other Activity to launch.

I would like clicking the trackpad to check the checkbox of the highlighted item. Is there a handler I can override to do this?

fredley
  • 32,953
  • 42
  • 145
  • 236
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • 1
    It could be a trackball or trackpad, nexus one or G2 for example. – blindstuff Oct 25 '10 at 18:22
  • Yes. The only thing I can think of is Overriding the onTouch handler and use that for launching the other Activity; and reserve onClick for the trackball (the phone I have is a pad and not a ball) – Andrew Oct 25 '10 at 18:22
  • My problem with that is the onTouch doesn't tell me what position – Andrew Oct 25 '10 at 18:32

2 Answers2

1

You need to override the onTrackballEvent(MotionEvent) method and catch ACTION_DOWN. Here is an example of how to do this:

@Override
    public boolean onTrackballEvent(MotionEvent event) {

        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            //Do your work here
            return true;
        }

        return super.onTrackballEvent(event);
    }

Hope this works for you!

mtmurdock
  • 12,756
  • 21
  • 65
  • 108
  • you might want to use getListView().getFocusedChild() to get the currently highlighted list item (returns the View for the list item) – mtmurdock Nov 05 '10 at 16:46
  • It's working! Though getFocusedChild() is not the correct method, it's getSelectedView(). The only issue I have is, in my app, checking the checkbox re-orders the list (using CursorAdapter), and the same item is selected. I'd like the next item in the list to be the selected item instead of maintaining the item selection. – Andrew Nov 05 '10 at 19:19
  • I've been able to manipulate this to select the correct item. Unfortunately, it looks bad because the list scrolls to the top and then back down to the selected item (using setSelection(int)). – Andrew Nov 05 '10 at 19:28
0

Not sure on a definite answer to this, but one thing worth researching is android:focusable.

I think your best bet is to make the List Items themselves not focusable, but the checkboxes focusable. This way when the user scrolls with the trackball/pad it will switch focus between the checkboxes instead of the list items, and behave in the way you want. This won't affect touch events.

fredley
  • 32,953
  • 42
  • 145
  • 236
  • I'll see what I can do. I seem to remember a problem where the checkboxes needed to not be focusable, otherwise users couldn't tap on a list item. – Andrew Nov 04 '10 at 13:15
  • I've just tested it out. If you have a CheckBox in a list item and the checkbox is focusable, you can not tap on the list item. I want the ability to finger tap on a list item and have it launch a screen, but I want the trackball/pad click to invoke different functionality. I'm hoping to be able to Override a handler, but it appears that a finger tap and a trackball/pad click invoke the same handler. – Andrew Nov 04 '10 at 13:52