LWUIT for Series 40 provides also solutions that can be used for advanced UI operations on the touch screen. Series 40 Gestures framework provides a number of gestures that can be used for different tap/press operations, or to drag, flick, or pinch. For drag and drop operations, there is also a LWUIT interface.
Some sequences of touch input are interpreted as gestures. You can use the LWUIT for Series 40 GestureHandler API to listen to the gestures supported by your target platform and add functionality to them.
The gestures supported by Series 40 full touch devices are:
Tap (press and release) |
Long press (press, hold and release) |
Long press repeated (repeated long press) |
Drag (press and move) |
Flick (press and move followed by release) |
Pinch (use two fingers to press, move fingers closer or further from each other) |
To listen to the gesture events, extend the LWUIT GestureHandler class. Override the gestureAction method to add functionality to different types of gesture events.
public class MyOwnGestureHandler extends GestureHandler { public void gestureAction(GestureEvent ge) { switch (ge.getType()) { case GestureInteractiveZone.GESTURE_RECOGNITION_START: // Add functionality here for the beginning of a gesture break; case GestureInteractiveZone.GESTURE_RECOGNITION_END: // Add functionality here for the end of a gesture break; case GestureInteractiveZone.GESTURE_TAP: // Add functionality here for tap gestures break; case GestureInteractiveZone.GESTURE_LONG_PRESS: // Add functionality here for long press gestures break; case GestureInteractiveZone.GESTURE_LONG_PRESS_REPEATED: // Add functionality here for repeated long press gestures break; case GestureInteractiveZone.GESTURE_DRAG: // Add functionality here for drag gestures break; case GestureInteractiveZone.GESTURE_FLICK: // Add functionality here for flick gestures break; case GestureInteractiveZone.GESTURE_PINCH: // Add functionality here for pinch gestures break; default: break; } } }
You can listen to gestures either globally, or only when a specific form is visible. Use static methods GestureHandler.setGlobalGestureHandler or GestureHandler.setFormGestureHandler to register your GestureHandler and set its scope. There can be only one global gesture handler at once.
public class GestureAwareForm extends Form { public GestureAwareForm() { MyOwnGestureHandler gestureHandler = new MyOwnGestureHandler(); GestureHandler.setFormGestureHandler(this, gestureHandler); } }
GestureHandler also supports listening to a single type of gestureEvent by giving the desired gesture type as a constructor parameter to the GestureHandler.
GestureHandler myHandler = new GestureHandler(GestureHandler.GESTURE_FLICK) { ... }
This will cause the GestureHandler to only receive flick events.
LWUIT's built-in drag and drop feature allows the user to tap and hold a Component, drag it to another location, and release the finger to drop the Component there. You can set Components as a draggable, and as a drop target that can handle dragged and dropped Components.
Figure: Drag and drop in LWUIT Demo
Use setDraggable(true) method to set a Component draggable. To set a Component as a drop target, call its setDropTarget(true) method. You can override the following methods for the drop target to add functionality for various drag and drop scenarios.
dragEnter(Component dragged)
This callback method indicates that a component drag has just entered this component.
dragExit(Component dragged)
This callback method provides an indication for a drop target that a drag operation is exiting the bounds of this component and it should clear any states that have been defined.
draggingOver(Component dragged, int x, int y)
This method allows a component to indicate if it is a drop target for the given component at the given x/y location (in component coordinate space).
drop(Component dragged, int x, int y)
Performs a drop operation of the component at the given X/Y location in coordinate space. This method should be overriden by subclasses to perform all of the logic related to moving a component; by default this method does nothing, and so dragging a component and dropping it has no effect.
The LWUIT Container class has a ready-made drop target implementation. Set the Components contained in a Container as draggable and the Container as drop target and you can reorder the Components within the Container by dragging and dropping.