mdv
mdv

Reputation: 499

Issue in android list

I have a list view and I am listening using Key listener for KEYCODE_5. The issue I am facing is whenever I press 5, the function onKey() (inside the listener) is called twice. Any idea?

Upvotes: 1

Views: 51

Answers (1)

Falle1234
Falle1234

Reputation: 5063

Could it be that you are not checking wether the the event was fired for keyup or keydown?

If you only need to catch the event for keydown you could create your eventhandler like this:

public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "5" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_5)) {
      // Perform action on key press
      // Your event code goes here
      return true;
    }
    return false;
}

Upvotes: 3

Related Questions