SlideShare a Scribd company logo
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

More Related Content

What's hot

Android Services
Android ServicesAndroid Services
Android Services
Ahsanul Karim
 
Android animation
Android animationAndroid animation
Android animation
Krazy Koder
 
android architecture
android architectureandroid architecture
android architecture
Aashita Gupta
 
android layouts
android layoutsandroid layouts
android layouts
Deepa Rani
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
Henry Osborne
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
Durai S
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
amaankhan
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
Aly Abdelkareem
 
Android studio installation
Android studio installationAndroid studio installation
Android studio installation
PoojaBele1
 
Android SDK Tutorial | Edureka
Android SDK Tutorial | EdurekaAndroid SDK Tutorial | Edureka
Android SDK Tutorial | Edureka
Edureka!
 
Android resources
Android resourcesAndroid resources
Android resources
ma-polimi
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layout
Krazy Koder
 
Android activity
Android activityAndroid activity
Android activity
Krazy Koder
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Android
guest213e237
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
DivyaKS12
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
Kan-Han (John) Lu
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
ma-polimi
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Android intent
Android intentAndroid intent
Android intent
Krazy Koder
 
Understaing Android EGL
Understaing Android EGLUnderstaing Android EGL
Understaing Android EGL
Suhan Lee
 

What's hot (20)

Android Services
Android ServicesAndroid Services
Android Services
 
Android animation
Android animationAndroid animation
Android animation
 
android architecture
android architectureandroid architecture
android architecture
 
android layouts
android layoutsandroid layouts
android layouts
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Android studio installation
Android studio installationAndroid studio installation
Android studio installation
 
Android SDK Tutorial | Edureka
Android SDK Tutorial | EdurekaAndroid SDK Tutorial | Edureka
Android SDK Tutorial | Edureka
 
Android resources
Android resourcesAndroid resources
Android resources
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layout
 
Android activity
Android activityAndroid activity
Android activity
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Android
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Android intent
Android intentAndroid intent
Android intent
 
Understaing Android EGL
Understaing Android EGLUnderstaing Android EGL
Understaing Android EGL
 

Viewers also liked

Journey of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, FacebookJourney of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, Facebook
DroidConTLV
 
Event Handling
Event HandlingEvent Handling
Event Handling
nationalmobileapps
 
The touch events
The touch eventsThe touch events
The touch events
Peter-Paul Koch
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and More
rogeryi
 
Android Custom Views
Android Custom ViewsAndroid Custom Views
Android Custom Views
Babar Sanah
 
Android Development - ConstraintLayout
Android Development - ConstraintLayoutAndroid Development - ConstraintLayout
Android Development - ConstraintLayout
Manuel Vicente Vivo
 
Ac sos
Ac sosAc sos
Introduction to Android Window System
Introduction to Android Window SystemIntroduction to Android Window System
Introduction to Android Window System
National Cheng Kung University
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
Dong-Ho Lee
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views
Lars Vogel
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even Handling
Oum Saokosal
 
[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers
Nikmesoft Ltd
 
[Android] Using Selection Widgets
[Android] Using Selection Widgets[Android] Using Selection Widgets
[Android] Using Selection Widgets
Nikmesoft Ltd
 
[Android] Data Storage
[Android] Data Storage[Android] Data Storage
[Android] Data Storage
Nikmesoft Ltd
 
Understanding Android Handling of Touch Events
Understanding Android Handling of Touch EventsUnderstanding Android Handling of Touch Events
Understanding Android Handling of Touch Events
jensmohr
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Bin Chen
 
Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)
Egor Elizarov
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUK
Fabio Collini
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
Fabio Collini
 
[iOS] Networking
[iOS] Networking[iOS] Networking
[iOS] Networking
Nikmesoft Ltd
 

Viewers also liked (20)

Journey of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, FacebookJourney of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, Facebook
 
Event Handling
Event HandlingEvent Handling
Event Handling
 
The touch events
The touch eventsThe touch events
The touch events
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and More
 
Android Custom Views
Android Custom ViewsAndroid Custom Views
Android Custom Views
 
Android Development - ConstraintLayout
Android Development - ConstraintLayoutAndroid Development - ConstraintLayout
Android Development - ConstraintLayout
 
Ac sos
Ac sosAc sos
Ac sos
 
Introduction to Android Window System
Introduction to Android Window SystemIntroduction to Android Window System
Introduction to Android Window System
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even Handling
 
[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers
 
[Android] Using Selection Widgets
[Android] Using Selection Widgets[Android] Using Selection Widgets
[Android] Using Selection Widgets
 
[Android] Data Storage
[Android] Data Storage[Android] Data Storage
[Android] Data Storage
 
Understanding Android Handling of Touch Events
Understanding Android Handling of Touch EventsUnderstanding Android Handling of Touch Events
Understanding Android Handling of Touch Events
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
 
Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUK
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
 
[iOS] Networking
[iOS] Networking[iOS] Networking
[iOS] Networking
 

Similar to [Android] Widget Event Handling

Open Intents - Android Intents Mechanism and Dependency Management
Open Intents - Android Intents Mechanism and Dependency ManagementOpen Intents - Android Intents Mechanism and Dependency Management
Open Intents - Android Intents Mechanism and Dependency Management
Friedger Müffke
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
Nikmesoft Ltd
 
Introduction to android - SpringPeople
Introduction to android - SpringPeopleIntroduction to android - SpringPeople
Introduction to android - SpringPeople
SpringPeople
 
Android session 2
Android session 2Android session 2
Android session 2
Ahesanali Suthar
 
Basic of Android App Development
Basic of Android App DevelopmentBasic of Android App Development
Basic of Android App Development
Abhijeet Gupta
 
Introduce Test Harness for Direct To Consumer Solutions.pdf
Introduce Test Harness for Direct To Consumer Solutions.pdfIntroduce Test Harness for Direct To Consumer Solutions.pdf
Introduce Test Harness for Direct To Consumer Solutions.pdf
Knoldus Inc.
 
Foss con2011
Foss con2011Foss con2011
Android Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdfAndroid Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdf
NomanKhan869872
 
Android ppt
Android pptAndroid ppt
Android Applications
Android ApplicationsAndroid Applications
Android Applications
Nazeer Hussain University
 
Android ppt
Android ppt Android ppt
Security on android
Security on androidSecurity on android
Security on android
pk464312
 
Mobile Application Development powerpoint
Mobile Application Development powerpointMobile Application Development powerpoint
Mobile Application Development powerpoint
JohnLagman3
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
weerabahu
 
Android101
Android101Android101
Android101
David Marques
 
Android introduction
Android introductionAndroid introduction
Android introduction
Durai S
 
android development training in mumbai
android development training in mumbaiandroid development training in mumbai
android development training in mumbai
faizrashid1995
 
Notes Unit3.pptx
Notes Unit3.pptxNotes Unit3.pptx
Notes Unit3.pptx
MIT Autonomous Aurangabad
 
Android
AndroidAndroid
Android
Tapan Khilar
 
Intro to android (gdays)
Intro to android (gdays)Intro to android (gdays)
Intro to android (gdays)
Omolara Adejuwon
 

Similar to [Android] Widget Event Handling (20)

Open Intents - Android Intents Mechanism and Dependency Management
Open Intents - Android Intents Mechanism and Dependency ManagementOpen Intents - Android Intents Mechanism and Dependency Management
Open Intents - Android Intents Mechanism and Dependency Management
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
 
Introduction to android - SpringPeople
Introduction to android - SpringPeopleIntroduction to android - SpringPeople
Introduction to android - SpringPeople
 
Android session 2
Android session 2Android session 2
Android session 2
 
Basic of Android App Development
Basic of Android App DevelopmentBasic of Android App Development
Basic of Android App Development
 
Introduce Test Harness for Direct To Consumer Solutions.pdf
Introduce Test Harness for Direct To Consumer Solutions.pdfIntroduce Test Harness for Direct To Consumer Solutions.pdf
Introduce Test Harness for Direct To Consumer Solutions.pdf
 
Foss con2011
Foss con2011Foss con2011
Foss con2011
 
Android Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdfAndroid Seminar BY Suleman Khan.pdf
Android Seminar BY Suleman Khan.pdf
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android Applications
Android ApplicationsAndroid Applications
Android Applications
 
Android ppt
Android ppt Android ppt
Android ppt
 
Security on android
Security on androidSecurity on android
Security on android
 
Mobile Application Development powerpoint
Mobile Application Development powerpointMobile Application Development powerpoint
Mobile Application Development powerpoint
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
 
Android101
Android101Android101
Android101
 
Android introduction
Android introductionAndroid introduction
Android introduction
 
android development training in mumbai
android development training in mumbaiandroid development training in mumbai
android development training in mumbai
 
Notes Unit3.pptx
Notes Unit3.pptxNotes Unit3.pptx
Notes Unit3.pptx
 
Android
AndroidAndroid
Android
 
Intro to android (gdays)
Intro to android (gdays)Intro to android (gdays)
Intro to android (gdays)
 

More from Nikmesoft Ltd

[iOS] Data Storage
[iOS] Data Storage[iOS] Data Storage
[iOS] Data Storage
Nikmesoft Ltd
 
[iOS] Multiple Background Threads
[iOS] Multiple Background Threads[iOS] Multiple Background Threads
[iOS] Multiple Background Threads
Nikmesoft Ltd
 
[iOS] Navigation
[iOS] Navigation[iOS] Navigation
[iOS] Navigation
Nikmesoft Ltd
 
[iOS] Basic UI Elements
[iOS] Basic UI Elements[iOS] Basic UI Elements
[iOS] Basic UI Elements
Nikmesoft Ltd
 
[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming
Nikmesoft Ltd
 
[Android] Multimedia Programming
[Android] Multimedia Programming[Android] Multimedia Programming
[Android] Multimedia Programming
Nikmesoft Ltd
 
[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android Animation
Nikmesoft Ltd
 
[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
Nikmesoft Ltd
 
[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers
Nikmesoft Ltd
 
[Android] Web services
[Android] Web services[Android] Web services
[Android] Web services
Nikmesoft Ltd
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
Nikmesoft Ltd
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services
Nikmesoft Ltd
 
[Android] Introduction to Android Programming
[Android] Introduction to Android Programming[Android] Introduction to Android Programming
[Android] Introduction to Android Programming
Nikmesoft Ltd
 

More from Nikmesoft Ltd (13)

[iOS] Data Storage
[iOS] Data Storage[iOS] Data Storage
[iOS] Data Storage
 
[iOS] Multiple Background Threads
[iOS] Multiple Background Threads[iOS] Multiple Background Threads
[iOS] Multiple Background Threads
 
[iOS] Navigation
[iOS] Navigation[iOS] Navigation
[iOS] Navigation
 
[iOS] Basic UI Elements
[iOS] Basic UI Elements[iOS] Basic UI Elements
[iOS] Basic UI Elements
 
[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming
 
[Android] Multimedia Programming
[Android] Multimedia Programming[Android] Multimedia Programming
[Android] Multimedia Programming
 
[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android Animation
 
[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
 
[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers[Android] Services and Broadcast Receivers
[Android] Services and Broadcast Receivers
 
[Android] Web services
[Android] Web services[Android] Web services
[Android] Web services
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services
 
[Android] Introduction to Android Programming
[Android] Introduction to Android Programming[Android] Introduction to Android Programming
[Android] Introduction to Android Programming
 

Recently uploaded

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 

Recently uploaded (20)

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 

[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 Event Handling Understanding Android Events 3
  • 4. 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
  • 6. Android Programming Widget Event Handling Event Listeners and Callback Methods 6
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. Android Programming Event Listeners and Callback Methods  Example 5.1  Example 5.1 project 17