Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T
Accepted Solution

Touch events how-to

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.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

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) {
			
		}

		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

 

Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Touch events how-to

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?

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Touch events how-to

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Touch events how-to

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(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() {

		}
    }
   
    
}

   And here's what I get. It still doesn't return to console any text.

  9550.jpg

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Touch events how-to

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 257
Registered: ‎05-05-2011
My Device: 9700
My Carrier: AT&T

Re: Touch events how-to

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(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");
			super.touchEvent(message);
			return false;
		}
		
		public HandleTouch() {

		}
    }
   
    
}

 Clip.jpgThanks for help!

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Touch events how-to

No problem.

With the return at the end of the function, do "return super.touchEvent(message);"
---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.