Handling User Event - Event Listeners

Kishan Menaria Oct 12 2020 · 1 min read
Share this

Event listener is an interface in the View class that contains a single callback method. When user interaction is encountered in UI, this method is called

Callback methods available in event listeners :

  • onClick() : This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.
  • onLongClick() : This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).
  • onFocusChange() : This is called when the user navigates onto or away from the item, using the navigation-keys or trackball.
  • onKey() : This is called when the user is focused on the item and presses or releases a hardware key on the device.
  • onTouch() : This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).
  • onCreateContextMenu() :  This is called when a Context Menu is being built (as the result of a sustained "long click"). See the discussion on context menus in the menus developer guide.  
  • The onClick() returns no value, but in most of cases it must return a Boolean. It is because:

  • onLongClick(): A Boolean is returned to indicate if event is done.
  • onKey(): A Boolean is returned to indicate stopping and event.
  • onTouch(): A Boolean is returned to indicate if listener consumes the event.
  • Hope you like this article don't forget to hit that like button :)

    Comments
    Read next