Events

Agenda

  • Handle events fired by views
  • Handle Activity events

Events

  • Your code is almost always run as the result of an event
    • User interactions on screen
    • Activity and operating system events
    • Except background threads
    • And Broadcasts
  • Events you already know
    • Activity event onCreate
    • Button click event
    • Returning from a second Activity

More event source examples

  • 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
  • System events
    • A SMS text message has been received
    • Battery level is low
    • User has chosen to share data with your app

Respond to an event

  • Event handlers
    • A method you provide that is called by system
    • Return quickly since user interface will be blocked
  • Override a method in a base class
    • Your class "extends" a base class and overrides one or more methods
  • Implement an interface
    • Your class "implements" an interface where methods are defined

Override a method in a base class

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

Example of overriding a method

  • onBackPressed is fired when back button is pressed
    public class MyActivity extends Activity {
      @Override
      public void onBackPressed() {
        super.onBackPressed();
      }
    }
    

Activity lifetime events

  • onCreate
    • When Activity is launched from a fresh object in memory
  • onStart
    • When user navigates back from other Activity
  • onResume
    • When focus is regained after showing a Dialog or similar
  • onPause
    • When about to lose focus from showing a Dialog or similar
  • onStop
    • When about to show some other Activity
  • onDestroy
    • When object is about to be destroyed from memory

Other Activity events

  • onBackPressed
    • When user pressed back button in current Activity
  • onActivityResult
    • When returning to this Activity after startForResult
  • onCreateOptionsMenu
    • When about to show ActionBar menu
  • onOptionsItemSelected
    • When user has chosen a command from ActionBar menu

Implement an interface

  • Places to put the implementation
    • Implement in existing Java class like your Activity
    • Implement in an anonymous class on-the-fly
    • Implement in a new class
  • All methods in the interface must be implemented
    • Many interfaces only have one method
    • Leave unused methods empty (will need return value)
  • Will not be called automatically just because it exists
    • Call a set-method on the object that fires the event
    • set-method named after interface

Implement in anonymous class

  • Tell Button to call method when event happens
    Button btn = (Button) findViewById(R.id.my_button);
    final String newText = "Ouch";
    btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        ((Button)view).setText(newText);
        MediaPlayer p = MediaPlayer.create(MainActivity.class R.raw.click);          
      }
    });
    
  • Note that
    • Accessing local variables outside of method requires final
    • Accessing outer "this" requires ClassName.this

General View event interfaces

  • OnClickListener
    • When user has clicked/touched+released inside the view
    • void onClick(View src)
  • OnTouchListener
    • When user touches, moves and releases inside the view
    • boolean onTouch(View src, MotionEvent event)
  • OnLongClickListener
    • When user has pressed and hold for an amount of time inside the view
    • boolean onLongClick(View src)

Spinner event

  • OnItemSelectedListener
    • When user has chosen an option from the drop down list
      void onItemSelected(AdapterView<?> src, View selectedView, 
        int selectedPosition, long selectedId)
      
    • When currently selected optionis unselected
      void onNothingSelected(AdapterView<?> src)
      

Introducing SeekBar

  • View with a knob moved between endpoints to set a value
    • An extension of the Progress view for showing long running operation status
  • android:max
    • Maximum value when knob moved to right-most position
  • android:progress
    • Start and current value between endpoints
  • Lowest/left-most is always zero

SeekBar events

  • OnSeekBarChangeListener
    void onStartTrackingTouch(SeekBar source)
    void onProgressChanged(SeekBar source, int pos, boolean byUser)
    void onStopTrackingTouch(SeekBar source)
    
  • onStartTrackingTouch
    • When the user first touches the knob, before moving it
  • onProgressChanged
    • When value is changed on each movement of knob
  • onStopTrackingTouch
    • When user releases the knob after movement
  • All methods must be defined, but can be empty

Touch events

  • When user interacts with screen
    • For detailed control over screen interaction
  • 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 while touching
      • ACTION_UP when user releases touch