05-21-2011 05:16 AM
Hi everyone,
recently I tried to play with touch events handling in 9550, and had several problems. The biggest one - it didn't react at my touch activity at all. Instead it throwed small menu "Full menu", you know. My goal is to trace X and Y positions and event type. I tried to use BB doc examples, but they simply didn't work. I know navigationClick works well, but what I want is not simply GUI - I want full control over touch events. Like in Angry birds or close. How do I do this?
My code is below. Now it outputs nothing.
package mypackage; import net.rim.device.api.system.Bitmap; import net.rim.device.api.system.EventInjector.TouchEvent; import net.rim.device.api.ui.Color; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Screen; import net.rim.device.api.ui.VirtualKeyboard; import net.rim.device.api.ui.component.BitmapField; import net.rim.device.api.ui.component.ButtonField; import net.rim.device.api.ui.component.Menu; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.VerticalFieldManag er; import net.rim.device.api.ui.decor.BackgroundFactory; public class MyScreen extends MainScreen { public MyScreen() { super(NO_SYSTEM_MENU_ITEMS); getScreen().getVirtualKeyboard().setVisibility(Vir tualKeyboard.HIDE_FORCE); add(new HandleTouch()); } class HandleTouch extends Field { protected void layout(int width, int height) { } public void paint(Graphics graphics) { //graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(), Bitmap.getBitmapResource("bg.png"), 0, 0); } 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; } return false; } public HandleTouch() { } } }
The process is so weird I guess it was the reason why Rovio didn't release Angry birds on BB ![]()
Solved! Go to Solution.
05-21-2011 06:20 AM
After many attempts I found that touchEvent never gets called. On the contrary, navigationClick works always fine. BTW - I added to the code a line:
public boolean isFocusable() { return true;}Anyway, touchEvent never performs anything - somehow touch events overcome it. How to "turn on" this method?
05-21-2011 10:28 AM
You seem to have everything right except one thing, your layout function is blank. Since you are simply doing a basic test, just call setExtent with the given width and height.
If layout setExtent is not called then it is a size-less field and the OS doesn't need to pass touch events (which are field-size based) to it. Other interaction events can still get to it since it is not size-related so it can access size-less fields.
05-21-2011 12:29 PM
Thanx for replying. During these hours I tried many different black and white magic combination but still can't make things work. Here's my last changes.
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(Vir tualKeyboard.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() { } } }
And here's what I get. It still doesn't return to console any text.
05-21-2011 01:53 PM
Hmm, I don't see anything that would make it "not" work. Well one but I don't think you would've done this.
What phone/simulator are you testing this on?
Also as a recommendation, be sure to use super.touchEvent(message) at the end if you don't handle anything.
05-21-2011 02:21 PM
Errrrrrrr, as often the solution was very hidden. By occasion Eclipse automatically imported this class:
import net.rim.device.api.system.EventInjector.TouchEvent;
which doesn't throw any notice or error in Eclipse, but it's the reason. Now, I just changed it to:
import net.rim.device.api.ui.TouchEvent;
For better view, here's the code. I also added snippet to prevent annoying "Full Menu", it's very useful.
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.component.Menu; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.TouchEvent; public class MyScreen extends MainScreen { //use this to kick off annoying menu pop-up public boolean onMenu(int instance) { return instance == Menu.INSTANCE_CONTEXT ? false : super.onMenu(instance); } public MyScreen() { super(NO_SYSTEM_MENU_ITEMS); getScreen().getVirtualKeyboard().setVisibility(Vir tualKeyboard.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"); super.touchEvent(message); return false; } public HandleTouch() { } } }
Thanks for help!
05-21-2011 10:41 PM