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
Developer
tylerjames
Posts: 42
Registered: ‎10-06-2008
My Device: Not Specified
Accepted Solution

Nature of touch events

Hello ladies and gents,

I'm currently working on enabling touch support for an existing application of mine and I'm running into a few difficulties. My MainScreen consists of a few managers: on the bottom I have a row of BitmapFields acting as buttons all contained in a HFM, above that I have a large EditField, and above that I have an HFM with some more selectable BitmapFields.

 

First, I'm not sure who should be implementing touchEvent(), my managers or the fields themselves. I've tried both but noticed some odd behaviour. It seems that the field that currently has focus receives the touchEvent regardless of where on the screen the touch event occurs.

I would think that the the OS would simply understand that simply touching a focusable object means "set focus to this object" and clicking on the object means "click on this object", I wouldn't think that you'd even need to implement the touchEvent() method to get this effect.

Does anyone have experience writing apps for the Storm? Am I missing something?

Thanks,
Tyler

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Nature of touch events

When you override touchEvent() in any Field subclass, you'll need to make sure that you pass any events along that do not belong to your field. Check the coordinates of the touch against the extent of the field. If you don't do this, then the focus will appear to be "stuck" on the current field.

 

This is a common issue for folks moving older code to the Storm. If you do some searching on this forum you'll find perhaps 20 threads discussing the same basic problem.

Developer
tylerjames
Posts: 42
Registered: ‎10-06-2008
My Device: Not Specified

Re: Nature of touch events

Thanks Rex,

I did a search on "touchEvent" but didn't find much.

 

Oops, looks like I just got it working.

Let me explain what I've got in case anyone else happens upon this thread:
- My screen has one "greater manager" that manages most items on the screen, including other managers

- I have a HFM at the bottom of the screen (which is managed by my greater manager) which contains several BitmapFields that are acting as buttons
- The issue I was having was trying to setFocus to the appropriate field when that field was touched

Here's what I added to my managers to handle the events:

protected boolean touchEvent(TouchEvent event)
{
//Figure out where the touch occurred on the screen
int globX = event.getGlobalX(1);
int globY = event.getGlobalY(1);

//Determine if the touch occurred within the extent of this manager
if (getExtent().contains(globX, globY))
{
if (event.getEvent() == TouchEvent.DOWN)
{
//Get the position of the touch within the manager
int x = event.getX(1);
int y = event.getY(1);

//See if there is a field at this screen position
int fieldIndex = getFieldAtLocation(x, y);

if ((fieldIndex >= 0) && (fieldIndex < getFieldCount()))
{
//Set the focus to the field at this position
Field f = getField(fieldIndex);
f.setFocus();
}

//Return false so that the event propagates to the contained field
return false;
}
}

//Event wasn't for us, handle in default manner
return super.touchEvent(event);
}



Also, you should probably make sure you set the extent of your managers properly, both real and virtual, I usually do this when I'm overriding the sublayout() method.

Hope this helps people.



 

Developer
olubeck
Posts: 56
Registered: ‎01-11-2009
My Device: Not Specified

Re: Nature of touch events

Thanks guys for the discussion...

 

I think that all of us who are having problems have a different model in mind...

 

I thought that the OS would do the functions you have in your code - the field onTouchEvent doesn't

even see the event unless there is a touch in its extent.... 

 

Also why does onTouchEvent require a true/false return value if it's not going to pass the event on to

other Fields in the hierarchy whose extent includes the event coordinates?

 

Olaf 

Developer
lucidbee
Posts: 86
Registered: ‎07-21-2009
My Device: Not Specified

Re: Nature of touch events

For what it's worth, this didn't quite work for me.

 

The problem was here:

 //Return false so that the event propagates to the contained field
return false;

 

That 'return false' messed it up.

When I deleted it, it worked.

 

 

New Contributor
badal
Posts: 4
Registered: ‎04-19-2010
My Device: BOLD

Re: Nature of touch events

Yes..I totally agree with you Olaf. I feel RIM have to improve its touch API.