Wednesday, April 27, 2011

hasPointerEvents wrong return

Touch enabled handsets are more and more common these days. Some does not even have a keyboard!

Custom User Interfaces should adapt to touch screens and, at least, display with bigger selectable items.

One easy way to know if your MIDlet is running on a touch enabled handset is to call Canvas.hasPointerEvents.

Unfortunately some Java ME Virtual Machines do not return true even if the handset DOES have a touch screen.

One workaround is to have a boolean attribute and set it to true if pointer pressed or released events are triggered:


class C extends Canvas {
boolean pointerEvents;
protected void pointerPressed(int x, int y) {
pointerEvents = true;
// treat the pointer pressed event
// ...
}
protected void pointerReleased(int x, int y) {
pointerEvents = true;
// treat the pointer released event
// ...
}
public boolean hasPointerEvents() {
return pointerEvents || super.hasPointerEvents();
}
}

This way the rest of your application UI might correctly adapt and present bigger items.

Related topics:

No comments: