Download presentation
Presentation is loading. Please wait.
Published byMaurice May Modified over 8 years ago
1
User Interaction Radan Ganchev Astea Solutions
2
Content Basic input events Gestures Drag and drop Sensors
3
Basic Input Events
4
Key events Track Touch events Other
5
Key Event Sources
7
Other event sources Touch events Trackball events
8
Other events Click ‣ Touch a clickable view ‣ Focus a view and press the action button ‣ Call view.performClick() Long click ‣ Same as click. It’s just... longer
9
Other events Focus change ‣ Use DPad to select another view ‣ Call view.requestFocus() Create context menu ‣ Long click a view which is registered for context menu
10
Handling events Register an event listener OnEventListener listener = new OnEventListener() { public void / boolean onEvent(...) {...} }; view.setOnEventListener(listener);
11
Handling events Register an event listener OnEventListener listener = new OnEventListener() { public void / boolean onEvent(...) {...} }; view.setOnEventListener(listener); Key Touch FocusChange CreateContextMenu Click LongClick
12
Handling events Register an event listener OnClickListener listener = new OnClickListener() { public void onClick(View view) {...} }; view.setOnClickListener(listener); Click listener example:
13
Handling events Extend View and override a method ‣ onKeyDown ‣ onKeyUp ‣ onTrackballEvent ‣ onTouchEvent ‣ onFocusChanged ‣ others...
14
The InputEvent object Holds the event parameters Comes in two flavors - KeyEvent and MotionEvent You receive it as an argument in your callback Some methods worth mentioning: ‣ getAction() ‣ getKeyCode() ‣ getX(), getY()
15
Event propagation Activity dispatch***Event
16
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch...
17
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch... bubble... bubble
18
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch... bubble... bubble View target =...; if (target.dispatchTouchEvent(...)) { return true ; } return onTouchEvent(...);
19
Gestures
20
What is a gesture? A series of touch events with pre-defined behavior Common gestures: FlingLong PressPinchScroll
21
Detecting Gestures The easy way: 1. Create a gesture listener - either implement OnGestureListener or extend SimpleOnGestureListener 2. Create a GestureDetector with your gesture listener 3. Pass all touch events of your view to the GestureDetector
22
Detecting Gestures The hard way: 1. Handle touch events for your view 2. Inspect the history of pointer movement and detect patterns - too messy for this lecture 3. Make sure no one steals your events - even messier... Involves methods like onInterceptTouchEvent and requestDisallowInterceptTouchEvent
23
Drag & Drop
24
Available since API level 11 (Android 3.0.x) Only works within a single application Allows users to move data (and other stuff) within your Activity layout
25
Drag & Drop Workflow Register OnDragListener s for each View you want to support drag & drop Call view.startDrag() whenever you decide, e.g. when view is long pressed Respond to the incoming drag events through your OnDragListener s
26
Drag & Drop Example
27
DRAG_STARTED startDrag()
28
Drag & Drop Example DRAG_STARTED DRAG_ENTERED startDrag()
29
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED startDrag()
30
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENTERED startDrag()
31
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENTERED DRAG_LOCATION startDrag()
32
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENDED DRAG_ENTERED DRAG_LOCATION DROP DRAG_ENDED startDrag()
33
Sensors
34
Android currently supports 11 types of sensors: ‣ Accelerometer ‣ Gyroscope ‣ Magnetic field sensor ‣ Ambient temperature sensor, etc...
35
Using Sensors Sensors are accessed through the SensorManager system service The workflow is as follows: 1. Obtain a reference to the SensorManager 2. Check if the sensor you need is present 3. Register a listener to activate the sensor 4. Unregister the listener when you’re done!
36
Q & A Questions? Feedback section: ‣ Did you hear well? ‣ Was there anything you didn’t understand? ‣ What would you like changed in our next lecture?
37
Resources Input Events GestureDetector documentation Drag and Drop SensorManager documentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.