Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Basic Functionality in Android. Functionality in Android Events in Java – mouse related mouse clicked button down or up mouse entered – many others key.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Object-Oriented Programming with Java The Java Event Model Lecture 5.
Event Handling Events and Listeners Timers and Animation.
1 Software Testing and Quality Assurance Lecture 26 (a) – Testing Interactions (Chapter 6)
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Object-Oriented Analysis and Design
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 lcdui Rob Pooley
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
User Interface Android Club Agenda Button OnClickListener OnLongClickListener ToggleButton Checkbox RatingBar AutoCompleteTextView.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
Android View Stuff. TextViews Display text Display images???
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
User Interaction Radan Ganchev Astea Solutions. Content Basic input events Gestures Drag and drop Sensors.
GUI Programming using Java - Event Handling
Events and Event Handling
CompSci 230 S Programming Techniques
Appendix I GUI Components and Event Handling
Responding to Events Event Handling in Java
Android – Event Handling
Chapter Eleven Handling Events.
Mobile Application Development BSCS-7 Lecture # 11
Programming in Java Event Handling
Android 16: Input Events Kirk Scott.
Chapter 15 Event-Driven Programming and Animations
1/10/2019 JavaFX Events COSC 330.
Android Topics Custom ArrayAdapters Creating an Event Listener
Event Driven Systems and Modeling
Android Developer Fundamentals V2
Constructors, GUI’s(Using Swing) and ActionListner
Android Project Structure, App Resources and Event Handling
Mobile Programmming Dr. Mohsin Ali Memon.
Presentation transcript:

Events

Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked– key typed –menu item selected– etc. The general approach is to capture events from the specific view object that the user interacts with.

Slide 3©SoftMoore Consulting Semantic versus Low-Level Events There is a distinction between low-level events and semantic or high-level events. A semantic event is a high-level encapsulation of what the user is doing. It usually encapsulates several low- level events. Example: Clicking a button is a semantic event. Low-level events include the left mouse button being pressed and released while “inside” the button area on the screen. Whenever possible, listen for semantic events.

Slide 4©SoftMoore Consulting Event Sources and Listeners Events are transmitted from event sources (e.g., buttons or scrollbars) to event listeners. An event source is an object that knows when/how the event occurs. Event sources report on events. An event listener is an object that needs to be notified when a certain event occurs in order to perform some action in response to the event.

Event Listeners An event listener is an interface nested within in the View class that contains a single callback method for handling a specific event. Example: Interface View.OnClickListener contains a single method, onClick(), that is called by the Android framework when the user clicks on the view object. Summary of Event Handling in Android –Create a class that implements the callback interface for that event; e.g., a class that handles button clicks. –Call a corresponding “ set ” method to register that class with the UI component to handle the event. Slide 5©SoftMoore Consulting

Example: Handling a Button Click (Preferred Approach) Button myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {... // actions when the button is clicked } }); Slide 6©SoftMoore Consulting This approach creates an instance of an anonymous inner class to implement the onClick() method. With Java 8, you should also be able to use a lambda expression, but lambda expressions are not yet supported by Android.

setOnClickListener() Sequence Diagram for the Example Slide 7©SoftMoore Consulting myButton : Button: OnClickListener user clicks the button onClick() : Activity «create» findViewById() event source event listener

Example: Handling a Button Click (A Second Alternative) public class MyActivity extends Activity implements View.OnClickListener { protected void onCreate(Bundle savedValues) {... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(this); } public void onClick(View v) {... // actions when the button is clicked }... } Slide 8©SoftMoore Consulting

Example: Handling a Button Click (A Second Alternative − continued) Limitation: Since there is only one onClick() method, MyActivity can respond in only one way to a click event. The onClick() method must contain a multiway branch (e.g., switch statement or nested if/else if statements) in order to handle clicks on different buttons. Slide 9©SoftMoore Consulting

Example: Handling a Button Click (A Third Alternative) // separate class (could be in same file as the Activity class) class MyOnClickListener implements View.OnClickListener { public void onClick(View v) {... // actions when the button is clicked } } // in the Activity class protected void onCreate(Bundle savedValues) {... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(new MyOnClickListener()); } Slide 10©SoftMoore Consulting

Common Event Listener Callback Methods onClick() –callback interface: View.OnClickListener –called by touching the item or by pressing enter when the item has focus onLongClick() –callback interface: View.OnLongClickListener –called when the user touches and holds the item onTouch() –callback interface: View.OnTouchListener –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. Slide 11©SoftMoore Consulting

Consuming Events Events are dispatched starting at the top object in the view hierarchy and then down the hierarchy until they reach the appropriate destination. The onClick() method does not return a value, but several callback methods return a boolean value to indicate whether or not the event has been “consumed”. A callback method returns true to indicate that the event has been handled (consumed) and that propagation of the event should stop at that point. Returning false indicates that the event has not been handled and that propagation should continue to other listeners in the hierarchy. Slide 12©SoftMoore Consulting

Relevant Links Input Events Buttons Slide 13©SoftMoore Consulting