1

I have a list in my app.

When a user touches a list item with their finger, the onListItemClick handler fires and I have it opening a new Activity.

However, when a user uses the trackball/pad to click, I want to perform some different functionality. I've overrode onTrackballEvent and everything works perfectly fine in the emulator's trackball mode.

Unfortunately, when testing on the Samsung Moment, clicking the trackpad fires the onListItemClick handler, not the onTrackballEvent handler.

Does anyone know why? Does anyone have a way around this?

Andrew
  • 20,756
  • 32
  • 99
  • 177

2 Answers2

1

In listview you can setOnKeyListener and do you code when keycode is KeyEvent.KEYCODE_DPAD_CENTER

listview.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
               switch(keyCode){
                 case KeyEvent.KEYCODE_DPAD_CENTER:
                    if(event.getAction()==KeyEvent.ACTION_UP){ //to do it only when key is released 
                   // do the code while trackball/pad is clicked
                    }
                   return true;
                 default:
                    return false;
                 }
              }
         }
});

It work for me. Hope this will give you some idea

Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • This almost works. When I press the pad down, my code executes. Unfortunately, when I release the button, my code executes a second time. So the code executes twice for each click of the trackball/pad. – Andrew Nov 23 '10 at 16:13
  • @Andrew From KeyEvent argument you can identify keyup and keydown. I have edited the code. Now it only executes once – Labeeb Panampullan Nov 24 '10 at 05:09
  • Haha, strange. This is better, but there is a new problem. When I do this, the Context Menu becomes activated! – Andrew Nov 24 '10 at 15:11
  • Maybe try changing it so it returns true on the "up" as well as the down, so that even though you don't use the "up" you also appear to consume that event instead of passing it on? – Chris Stratton Nov 24 '10 at 17:25
-1

Try overriding the dispatchTrackball event and grab those events. Also, just a suggestion - I would not use a different action on the trackball because not all Android phones have the trackball.

Vuk
  • 1,225
  • 12
  • 15