Handling User Interaction Events in Android

For your new View to be interactive, it will need to respond to user-initiated events such as key presses, screen touches, and button clicks. Android exposes several virtual event handlers that you can use to react to user input:

onKeyDown — Called when any device key is pressed; includes the D-pad, keyboard, hang-up, call, back, and camera buttons

onKeyUp — Called when a user releases a pressed key

onTrackballEvent — Called when the device’s trackball is moved

onTouchEvent — Called when the touchscreen is pressed or released, or when it detects movement

Input event handling for Views

@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
 // Return true if the event was handled.
 return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
 // Return true if the event was handled.
 return true;
}
@Override
public boolean onTrackballEvent(MotionEvent event ) {
 // Get the type of action this event represents
 int actionPerformed = event.getAction();
 // Return true if the event was handled.
 return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
 // Get the type of action this event represents
 int actionPerformed = event.getAction();
 // Return true if the event was handled.
 return true;
}

Leave a Comment