User Interaction Radan Ganchev Astea Solutions
Content Basic input events Gestures Drag and drop Sensors
Basic Input Events
Key events Track Touch events Other
Key Event Sources
Other event sources Touch events Trackball events
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
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
Handling events Register an event listener OnEventListener listener = new OnEventListener() { public void / boolean onEvent(...) {...} }; view.setOnEventListener(listener);
Handling events Register an event listener OnEventListener listener = new OnEventListener() { public void / boolean onEvent(...) {...} }; view.setOnEventListener(listener); Key Touch FocusChange CreateContextMenu Click LongClick
Handling events Register an event listener OnClickListener listener = new OnClickListener() { public void onClick(View view) {...} }; view.setOnClickListener(listener); Click listener example:
Handling events Extend View and override a method ‣ onKeyDown ‣ onKeyUp ‣ onTrackballEvent ‣ onTouchEvent ‣ onFocusChanged ‣ others...
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()
Event propagation Activity dispatch***Event
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch...
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch... bubble... bubble
Event propagation Activity dispatch***Event Window dispatch DecorView Target View dispatch... bubble... bubble View target =...; if (target.dispatchTouchEvent(...)) { return true ; } return onTouchEvent(...);
Gestures
What is a gesture? A series of touch events with pre-defined behavior Common gestures: FlingLong PressPinchScroll
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
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
Drag & Drop
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
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
Drag & Drop Example
DRAG_STARTED startDrag()
Drag & Drop Example DRAG_STARTED DRAG_ENTERED startDrag()
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED startDrag()
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENTERED startDrag()
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENTERED DRAG_LOCATION startDrag()
Drag & Drop Example DRAG_STARTED DRAG_ENTERED DRAG_LOCATION DRAG_EXITED DRAG_ENDED DRAG_ENTERED DRAG_LOCATION DROP DRAG_ENDED startDrag()
Sensors
Android currently supports 11 types of sensors: ‣ Accelerometer ‣ Gyroscope ‣ Magnetic field sensor ‣ Ambient temperature sensor, etc...
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!
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?
Resources Input Events GestureDetector documentation Drag and Drop SensorManager documentation