I try to implement simple touch event handling on Blackberry 9550 emulator, but it doesn’t work. Actually, touchEvent never gets called, ‘cos no text ever appears in the console. Also, I get an annoying “Full Menu” which appears on touching the screen.
Here’s the code:
package mypackage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EventInjector.TouchEvent;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.VirtualKeyboard;
import net.rim.device.api.ui.container.MainScreen;
public class MyScreen extends MainScreen
{
public MyScreen()
{
super(NO_SYSTEM_MENU_ITEMS);
getScreen().getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE_FORCE);
add(new HandleTouch());
}
class HandleTouch extends Field {
protected void layout(int width, int height) {
setExtent(width, height);
}
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(), Bitmap.getBitmapResource("bg.png"), 0, 0);
}
public boolean isFocusable() { return true;}
protected boolean touchEvent(TouchEvent message) {
switch( message.getEvent() ) {
case TouchEvent.CLICK:
System.out.println("----------------------------->CLICK");
return true;
case TouchEvent.DOWN:
System.out.println("----------------------------->DOWN");
return true;
case TouchEvent.MOVE:
System.out.println("----------------------------->MOVE");
return true;
}
System.out.println("PRINT ME SOMETHING IN ANY CASE");
return false;
}
public HandleTouch() {
}
}
}
1). First of all, with this code
you are actually setting a VERY large size of the field. This because the BB UI framework passes max available/possible dimentions to
layout(int width, int height)so the field should use some part within the passed values. In this specific case the width will be the width of the display (360 px) and the height is the max possible height of theVerticalFieldManager(the one your are adding screen fields to, it is implicitly present in the screen’s internals) (1073741823 px). So, finally this may result in a very largeBitmapobject that is required with the field in order to be painted and you can get an uncaught error “Bitmap is too large” (I did on Storm 9530).So, the
layout()should use some relatively small values, e.g.:2).
Well, actually it does get called. To see that you should simply touch (versus click). Left button of the mouse simulates clicks (a sequence of
TouchEvent.DOWN>TouchEvent.CLICK>TouchEvent.UNCLICK>TouchEvent.UP), right button simulates touches (a sequence ofTouchEvent.DOWN>TouchEvent.UP).3).
This is because your field does not consume
TouchEvent.UNCLICKevent. For instance, with this code your field will not show the popup:But, that is a bad solution for the popup. It is better to understand what really causes the popup. If
TouchEvent.UNCLICKevent is not consumed then BB UI framework callsgetContextMenu(int instance)andmakeContextMenu(ContextMenu contextMenu, int instance)methods of the field. So in order to disable the popup (which is actually aContextMenucreated by thegetContextMenu(int instance)you should override thegetContextMenu(int instance)to be smth like this:4). Finally I’d recommend to not change native/default behavior of
touchEvent(TouchEvent message)method. You can just watch/log it, but don’t change (always call its super version). This is because touch events handling is more complicated than it may look at first. It is very easy to get a tricky bug here. I do believe most programmers should not change the native behavior oftouchEvent(TouchEvent message)unless they really want to create some custom UI component to work with touch gestures. Normally they just want to react on a click (to behave as aButtonField), however for that you can simply overridenavigationClick(int status, int time)ornavigationUnclick(int status, int time). The BB UI framework will call those methods when user clicks your field on a touch screen.