0% found this document useful (0 votes)
28 views10 pages

03.1 Event Handling - 1X2

Uploaded by

niyaca4113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

03.1 Event Handling - 1X2

Uploaded by

niyaca4113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Event Handling Dr. Harshad Prajapati 14 June 2023 Event Handling e Event handling involves three things: Source (generates event): m Android component (generally Ul) that generates an event when some changes happen, e.g., user clicks. Event handler (handles event): PRN e SM eae ht cence Run Cuaect CMR MEST Tu on ovale sXe) Ure ele) A AMT Le ML Lele Ceo Ceo eee ea Celok e We already know event handling in Java. Event handling in Java ® XListener interface specifies contract(s) for event handling logic. For example, ActionListener specifies actionPerformed(ActionEvent e) as a contract. e Button (java.awt.Button) component is a source that generates event. e Registration of event handling logic (implementation of ActionListener, romeo MMe RTT GReN cues cers) For example, using button.addActionListener(alObj); In place of alObj (action listener object), we can pass an anonymous object of an anonymous class that implements ActionListener. Event handling in Java mare U1 oleae? (mn asec btnSubmit.addActionListener(new ActionListener() { public voj actionPerformed(ActionEvent ae){ Jlevent handling logic ) ea ticoy Searcy vena Event Handling in Android ® Event handling in Android is done in similar way. e Example of event source: android.widget.Button e Example of event handler: Implementation of View.OnClickListener: CMe lae ol AWA One em) nae UA Soa) contract between event source and event handler. e Example of event registration: btnSubmit.setOnClickListener(object of View.OnClickListener) Method names for registration e Java uses method names starting with addXListener. The word add indicates, we can register more than one listener with event source for the same event. e Android uses method names starting with setXListener. The word set indicates, we can register exactly one listener with event source for the same event. If event handler was already registered, it would be replaced by the new one on calling setXListener. Listeners in Android classes are Static Cees aeolian ie cee MUS) al ma CAC el) Ce associated class. e Any inner interface in Java is static by default. Such interfaces are implicitly static even if static keyword is not used. © Static inner interfaces can be accessed directly using the enclosing class name. We do not require an instance of the enclosing class. Miia ROMO -en el (oe) ama CnCleeNCl Clen CU) Tatil SRR m View.OnClickListener. Some Widely Use Listeners ¢ View.OnClickListener: It handles click events on a View. The event handling method is onClick(View v) e View.OnLongClickListener: It handles long-click events on a View. The event handling method is onLongClick(View v) Some Widely Use Listeners CAC Melon a Cp It handles touch events on a View. Event handling methods are m onTouch(View v, MotionEvent event) onDoubleTap(MotionEvent e) onSwipeDown() onSwipeLeft() onSwipeUp() onSwipeRight() Some Widely Use Listeners e ScaleGestureDetector.OnScaleGestureListener: It handles events related to gestures. (E.g., pinch zoom in/out) Event handling methods are m onScale(ScaleGestureDetector detector): * lItresponds to scaling events for a gesture in progress. m= onScaleBegin(ScaleGestureDetector detector) PM ees NCR ICN ClaTteReme Reet Re ssi) m onScaleEnd(ScaleGestureDetector detector) e Itresponds to the end of a scale gesture. Some Widely Use Listeners © ScaleGestureDetector.OnScaleGestureListener: Bede selneT creh RUHR unl ite Regs cig m One onScaleBegin() mu Zero or more onScale() m One onScaleEnd() Some Widely Use Listeners e AdapterView.OnltemClickListener: Hala Talel [mel aa oe ae =) ACU oe oi SA GridView. The event handling method is onltemClick(AdapterView parent, View view, int position, long id) e DatePickerDialog.OnDateSetListener: Handles the selection of a date in a DatePickerDialog. The event handling method is onDateSet(DatePicker view, int year, int month, int dayOfMonth) Some Widely Use Listeners © TimePickerDialog.OnTimeSetListener: It handles the selection of a time in a TimePickerDialog. The event handling method is onTimeSet(TimePicker view, int hourOfDay, int minute) e TabLayout.OnTabSelectedListener: It handles tab selection events in a TabLayout component. Event handling methods are m onTabSelected(TabLayout.Tab tab) m onTabUnselected(TabLayout.Tab tab) = onTabReselected(TabLayout Tab tab) Some Widely Use Listeners e ViewPager.OnPageChangeListener: It handles page change events in a ViewPager. Event handling method are m_ onPageSelected(int position) m onPageScrolled(int position, float positinOffset, int PSM ise ancl) m onPageScrollStateChanged(int state) Some Widely Use Listeners e CompoundButton.OnCheckedChangeListener: It handles events related to RadioButton and CheckBox. The event handling method is m onCheckedChanged(CompoundButton buttonView, boolean isChecked) Some Widely Use Listeners e TextWatcher: It is a standalone interface. Haar Walel enero OM mela y ce Event handling methods are mu beforeTextChanged(CharSequence s, int start, int count, int after) m onTextChanged(CharSequence s, int start, int before, int count) = afterTextChanged(Editable s) Event Registration methods for any View (Button, TextView setOnClickListener: Callback when the view is clicked. setOnDragListener: Callback when the view is dragged setOnFocusChangeListener: Callback when the view changes focus. setOnGenericMotionListener: Callback for arbitrary gestures. setOnHoverListener: Callback for hovering over the view. setOnKeyListener: Callback for pressing a hardware key when view has focus. setOnLongClickListener: Callback for pressing and holding a view. setOnTouchListener: Callback for touching down or up on a view. Event Registration methods for AdapterView setOnltemClickListener: Callback when an item contained is clicked. setOn|temLongClickListener: Callback when an item contained is clicked and Lato setOnltemSelectedListener: Callback when an item is selected. Event Registration methods for EditText PME MSO ile ee Mc erl Cala un eNom ee TlueReurlteceh e setOnEditorActionListener: Callback when an action button on the soft keyboard is pressed. oie oaLero) e https://guides.codepath.com/android

You might also like