Events Presentation

Events Presentation


  • Know how to respond to events in the user interface
  • Know how to react to Activity events
  • Know how to catch Broadcasts


  • Everything that happens in an Android app is the result of
    • An event being fired by the system, possibly from user interaction
    • A method being called to respond to that event
    • Except if you start your own threads to run in parallell
  • You've already responded to at least two events in your apps
    • The creation of the Activity sent as an event to the onCreate method
    • The click of an Button sent as an event to the specified onClick method

Some more examples of events

  • Activity events
    • Back button was pressed
    • A menu item was chosen by the user
    • A second Activity finishes and returns the result
  • View events
    • An item was selected in a Spinner
    • A button was clicked for a longer period of time
    • The user has touched somewhere in the View
  • System events
    • A SMS text message has been received
    • Battery level is low
    • The user has chosen to share something from another app with your app

Respond to an event

  • Event handlers
    • You've already seen that an event handler is a Java method that is called when the event occurs
    • All event handlers should return as quickly as possible since the user interface will be blocked
  • Override a method in a base class
    • Your class "extends" a base class and can then override the event handler methods in the base class
    • Can choose any number of supported event handlers to override
  • Implement an interface
    • Your class "implements" an interface and can then implement the event handler methods defined by the interface
    • Must implement all event handlers in interface, but can just leave them empty if not relevant

Override a method in a base class

  • Must have the exact same definition (name, return type and arguments)
  • Should use @Override attribute to have Java compiler check its base existance
  • You are replacing the base method with your own method
  • Might need or want to call base method too

Example of overriding a method in a base class

  • onBackPressed is fired when back button is pressed on device
    public class MyActivity extends Activity {
      public void onBackPressed() {

Implement an interface

  • Places to put the implementation
    • Implement in a Java class like your Activity
    • Implement in an anonymous class on-the-fly
  • All methods in the interface must be implemented
    • Many interfaces only have one method
    • If you don't need to react to some of them, just leave them empty
  • Will not be called automatically just because it exists
    • Call a set-method on the object that fires the event
    • Different set-methods for different interfaces

Example of implementing interface in anonymous class

  • Tell Button to call your anonymous class method when event fires
    Button btn = (Button) findViewById(;
    final String newText = "Ouch";
    btn.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        Toast.makeText(MainActivity.this, "Yes, I made it!", Toast.LENGTH_SHORT).show();
  • If event handler access variables outside of itself, they need to be final or declared as class variable
  • For access to the callers this, use class name prefix like MainActivity.this

Activity lifetime events

  • Events fired by the Activity and handled by overriding methods in your own Activity class
  • onCreate
    • Already used. Fired once when the Activity is first created.
  • onStart
    • Fired when the user navigates back to the Activity after running some other Activity
  • onResume
    • Fired when the Activity receives focus again after losing it to something like a Dialog and also as a follow up to onStart
  • onPause
    • Fired when the Activity loses focus, either because of something like a Dialog or because of the user navigating to another Activity
  • onStop
    • Fired when the user navigates to another Activity
  • onDestroy
    • Fired when the Activity is about to be destroyed (exiting/haven't been used in a while)

Other Activity events

  • onBackPressed
    • Fired when the physical back button of the device is pressed. (No event exists for the physical home button.)
  • onActivityResult
    • Fired when a second Activity finishes and control is returned to the launching Activity
  • onCreateOptionsMenu, onOptionsItemSelected
    • Fired when creating the ActionBar menu items and when a menu item has been chosen by the user

General View event interfaces

  • OnClickListener
    • This event is fired when the user has touched and released a finger on the view
    • void onClick(View src)
  • OnTouchListener
    • This event is fired when the user touches, moves and releases any amount of fingers on the view
    • boolean onTouch(View src, MotionEvent event)
  • OnLongClickListener
    • This event is fired when the user touches and holds an amount of time on the view
    • boolean onLongClick(View src)

Spinner event

  • OnItemSelectedListener
    • This event is fired when the user has chosen an option from the drop down list
  • Two methods in interface
    • void onItemSelected(AdapterView<?> src, View selectedView, int selectedPosition, long selectedId)
      • Depending on how the entries of the Spinner was specified, there might not be any selectedId that is valid. Best to use the selectedPosition if the order of options is well known.
    • void onNothingSelected(AdapterView<?> src)
      • Not applicable to a Spinner. The event interface is shared with other visual elements where such an event is valid.


  • Visual element with a knob that you move between endpoints to set a value
    • Actually extended from the base Progress element that has no interaction and only shows some sort of visual progress bar while waiting for an operation to complete
  • Attributes
    • android:max
      • The maximum value that corresponds to moving the knob to the right-most position
    • android:progress
      • The default value to start at (zero is always the value when the knob is in the left-most position)

SeekBar event interfaces

  • OnSeekBarChangeListener
    • void onStartTrackingTouch(SeekBar source)
      • Called when the user first touches the knob, before moving it
    • void onProgressChanged(SeekBar source, int pos, boolean byUser)
      • Called on every movement of the knob
    • void onStopTrackingTouch(SeekBar source)
      • Called when the user has released the knob and the operation is finished
  • Even if you only need to catch onProgressChanged, all methods must be defined (but can be empty)

Touch events

  • Low level touch events are sent as single method call
    • Argument to method call contains more information
  • MotionEvent object
    • getX() has x position of event
    • getY() has y position of event
    • getAction() has reason for event
      • ACTION_DOWN when user first touches the screen
      • ACTION_MOVE when user moves touch without releasing
      • ACTION_UP when user releases the touch from the screen

Android Studio assistance

  • Override a method
    • Press Ctrl+O
    • Select method and press OK/Enter
  • Generate all methods in interface for anonymous class
    • Type code for the setOn-method call
    • Type new as the argument to the setOn-method call
    • Select interface from drop down list (show with Ctrl+Space)
    • All interface methods will be generated
  • Will show lambda expressen syntax for single anonymous class implementations
    • Can show it like "(view, motion) -> { }" instead of the actual Java declaration