Android Programming




        Lesson 5
Widget Event Handling

      NGUYEN The Linh
Android Programming


Contents


     1     Understanding Android Events

     2     Event Listeners and Callback Methods


     3     Exercise 5




                         2
Android Programming


Widget Event Handling




     Understanding Android Events




                  3
Android Programming


Understanding Android Events

 Events in Android can take a variety of different forms,
  but are usually generated in response to an external
  action.

 The most common form of events, involve some form
  of interaction with the touch screen. Such events fall
  into the category of input events.




                           4
Android Programming


Understanding Android Events




                    5
Android Programming


Widget Event Handling




   Event Listeners and Callback Methods




                    6
Android Programming


Event Listeners and Callback Methods

 onClick()
    From View.OnClickListener. 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.




                                 7
Android Programming


Event Listeners and Callback Methods

 onLongClick()
    From View.OnLongClickListener. 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).




                                 8
Android Programming


Event Listeners and Callback Methods

 onFocusChange()
    From View.OnFocusChangeListener. This is called when the
     user navigates onto or away from the item, using the navigation-
     keys or trackball.

 onKey()
    From View.OnKeyListener. This is called when the user is
     focused on the item and presses or releases a hardware key on
     the device.



                                 9
Android Programming


Event Listeners and Callback Methods

 onTouch()
    From View.OnTouchListener. 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).




                                  10
Android Programming


Event Listeners and Callback Methods

 Using a separate Listener class
    Approach
      • Use an external class that implements View.OnClickListener
      • Import android.view.View.OnClickListener, then say “implements
        OnClickListener”
    Advantages
      • You can pass arguments to change behavior
      • if event handler can be applied to different controls, it can be change
        independently from rest of app.
    Disadvantages
      • If you want to call code in main Activity, you need reference
      • Even then, that code in main Activity must be public
                                       11
Android Programming


Event Listeners and Callback Methods

 Using a Named Inner Class for Event Handling
    Approach
      • Use an inner class that implements View.OnClickListener
    Advantages
      • You can pass arguments to change behavior.
      • Event handler methods can access private data of Activity. No reference is
        needed to call to Activity.
    Disadvantages
      • Since Listener class is in same file as Activity, it is more tightly coupled, and
        cannot be changed independently.



                                        12
Android Programming


Event Listeners and Callback Methods

 Using an Anonymous Inner Class for Event Handling
    Approach
     • Use an anonymous inner class that implements the Listener
   Advantages
     • Assuming that each class is applied to a single control only, same advantages
       as named inner classes, but shorter.
   Disadvantages
     • If you applied the handler to more than one control, you would have to cut and
       paste the code for the handler.
          – This approach should be applied for a single control only
     • If the code for the handler is long, it makes the code harder to read by putting
       it inline.

                                              13
Android Programming


Event Listeners and Callback Methods

 Handling Events by Having Main Activity Implement
  Listener Interface
    Approach
      • Have the main Activity implement the Listener interface. Put the handler
        method in the main Activity. Call setOnClickListener(this).
    Advantages
      • Assuming that the app has only a single control of that Listener type, this is
        the shortest and simplest of the approaches.
    Disadvantages
      • This approach should be applied when your app has only a single control of
        that Listener type
      • You cannot pass arguments to the Listener.
                                       14
Android Programming


Event Listeners and Callback Methods

 Handling Events by Specifying the Event Handler
  Method in main.xml
    Approach
      • Put the handler method in the main Activity. Do not implement a Listener
        interface or call setOnClickListener. Have the layout file (main.xml) specify
        the handler method via the android:onClick attribute.
    Advantages
      • Assuming that the app has only a single control of that Listener type, mostly
        the same advantages (short/simple code) as the previous approach where the
        Activity implemented the interface.




                                       15
Android Programming


Event Listeners and Callback Methods

 Handling Events by Specifying the Event Handler
  Method in main.xml(cont.)
    Advantages(cont.)
      • More consistent with the “do layout in XML” strategy
      • You can supply different method names for different controls, so not nearly as
        limited as interface approach.
    Disadvantages
      • You cannot pass arguments to Listener
      • Less clear to the Java developer which method is the handler for which control
      • Since no @Override, no warning until run time if method is spelled wrong or
        has wrong argument signature


                                      16
Android Programming


Event Listeners and Callback Methods

 Example 5.1
    Example 5.1 project




                           17
Android Programming


Exercise 5




             18
Android Programming

[Android] Widget Event Handling

  • 1.
    Android Programming Lesson 5 Widget Event Handling NGUYEN The Linh
  • 2.
    Android Programming Contents 1 Understanding Android Events 2 Event Listeners and Callback Methods 3 Exercise 5 2
  • 3.
    Android Programming Widget EventHandling Understanding Android Events 3
  • 4.
    Android Programming Understanding AndroidEvents  Events in Android can take a variety of different forms, but are usually generated in response to an external action.  The most common form of events, involve some form of interaction with the touch screen. Such events fall into the category of input events. 4
  • 5.
  • 6.
    Android Programming Widget EventHandling Event Listeners and Callback Methods 6
  • 7.
    Android Programming Event Listenersand Callback Methods  onClick()  From View.OnClickListener. 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. 7
  • 8.
    Android Programming Event Listenersand Callback Methods  onLongClick()  From View.OnLongClickListener. 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). 8
  • 9.
    Android Programming Event Listenersand Callback Methods  onFocusChange()  From View.OnFocusChangeListener. This is called when the user navigates onto or away from the item, using the navigation- keys or trackball.  onKey()  From View.OnKeyListener. This is called when the user is focused on the item and presses or releases a hardware key on the device. 9
  • 10.
    Android Programming Event Listenersand Callback Methods  onTouch()  From View.OnTouchListener. 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). 10
  • 11.
    Android Programming Event Listenersand Callback Methods  Using a separate Listener class  Approach • Use an external class that implements View.OnClickListener • Import android.view.View.OnClickListener, then say “implements OnClickListener”  Advantages • You can pass arguments to change behavior • if event handler can be applied to different controls, it can be change independently from rest of app.  Disadvantages • If you want to call code in main Activity, you need reference • Even then, that code in main Activity must be public 11
  • 12.
    Android Programming Event Listenersand Callback Methods  Using a Named Inner Class for Event Handling  Approach • Use an inner class that implements View.OnClickListener  Advantages • You can pass arguments to change behavior. • Event handler methods can access private data of Activity. No reference is needed to call to Activity.  Disadvantages • Since Listener class is in same file as Activity, it is more tightly coupled, and cannot be changed independently. 12
  • 13.
    Android Programming Event Listenersand Callback Methods  Using an Anonymous Inner Class for Event Handling  Approach • Use an anonymous inner class that implements the Listener  Advantages • Assuming that each class is applied to a single control only, same advantages as named inner classes, but shorter.  Disadvantages • If you applied the handler to more than one control, you would have to cut and paste the code for the handler. – This approach should be applied for a single control only • If the code for the handler is long, it makes the code harder to read by putting it inline. 13
  • 14.
    Android Programming Event Listenersand Callback Methods  Handling Events by Having Main Activity Implement Listener Interface  Approach • Have the main Activity implement the Listener interface. Put the handler method in the main Activity. Call setOnClickListener(this).  Advantages • Assuming that the app has only a single control of that Listener type, this is the shortest and simplest of the approaches.  Disadvantages • This approach should be applied when your app has only a single control of that Listener type • You cannot pass arguments to the Listener. 14
  • 15.
    Android Programming Event Listenersand Callback Methods  Handling Events by Specifying the Event Handler Method in main.xml  Approach • Put the handler method in the main Activity. Do not implement a Listener interface or call setOnClickListener. Have the layout file (main.xml) specify the handler method via the android:onClick attribute.  Advantages • Assuming that the app has only a single control of that Listener type, mostly the same advantages (short/simple code) as the previous approach where the Activity implemented the interface. 15
  • 16.
    Android Programming Event Listenersand Callback Methods  Handling Events by Specifying the Event Handler Method in main.xml(cont.)  Advantages(cont.) • More consistent with the “do layout in XML” strategy • You can supply different method names for different controls, so not nearly as limited as interface approach.  Disadvantages • You cannot pass arguments to Listener • Less clear to the Java developer which method is the handler for which control • Since no @Override, no warning until run time if method is spelled wrong or has wrong argument signature 16
  • 17.
    Android Programming Event Listenersand Callback Methods  Example 5.1  Example 5.1 project 17
  • 18.
  • 19.