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
New Contributor
noone77
Posts: 3
Registered: ‎12-25-2010
My Device: Developer
Accepted Solution

BitmapField consumes all clicks on the screen

Hi,

 

I hope someone can help me with this problem: I have a screen on which only one BitmapField is on. When clicking on this BitmapField "something" should happen which works fine.

The problem is that even if I click somewhere else on the screen the "Click" is triggered.

This is the main code on the screen:

 

 

VerticalFieldManager view = new VerticalFieldManager();
    
Bitmap bm = Bitmap.getBitmapResource("image.jpg"); // size 60x60
MyBitmapField bmf = new MyBitmapField(bm, BitmapField.FOCUSABLE);
bmf.setChangeListener(this);
view.add(bmf);
add(view);

 And this the implementation of the "MyBitmapField":

 

 

public class MyBitmapField extends BitmapField
{
  public MyBitmapField(Bitmap bitmap)
  {
    super(bitmap);
  }

  public MyBitmapField(Bitmap bitmap, long style)
  {
    super(bitmap, style);
  }
  
  protected boolean navigationClick(int status, int time)
  {
    FieldChangeListener listener = getChangeListener();
    if (null != listener)
    {
      Dialog.alert("Clicked");
      listener.fieldChanged(this, 1);
      return true;
    }

    return super.navigationClick(status, time);
  }

  protected boolean touchEvent(TouchEvent message) 
  {
    /*
    if (TouchEvent.CLICK == message.getEvent()) 
    {
      int index = this.getManager().getFieldAtLocation(message.getX(1), message.getY(1));
      if (index == -1)
          return super.touchEvent(message);
      Field field = this.getManager().getField(index);
      if (field != this)
        return super.touchEvent(message);
      
      FieldChangeListener listener = getChangeListener();
      if (null != listener)
        listener.fieldChanged(this, 1);
    }*/
    return super.touchEvent(message);
}

}

 In the touchEvent method is something I also tried - but it didn't work either.

 

The fieldChanged handler is as normal:

 

  public void fieldChanged(Field field, int arg)
  {
    if (field instanceof MyBitmapField)
    {
      MyBitmapField bmf = (MyBitmapField) field;
      Dialog.alert("Clicked: " + Integer.toString(arg));
    }
  }

 

I'm trying on the 9550 Simulator. 

 


Can someone please help me and tell me what I need to do to prevent that the click event is triggered even if I click outside of the BitmapField (I also put a border around, just to make it visible it is not full size).

Thanks a lot

 

 

Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: BitmapField consumes all clicks on the screen

This is exactly how BlackBerry works, even though it is quite buggy IMHO: the CLICK touch event is translated into navigationClick() on the currently focused field. The problem lies in the fact that DOWN event moves focus to the field under it only if there is a focusable field where the screen is touched, otherwise the focus stays where it is.

 

In order to overcome this, program your touchEvent similar to this:

 

protected boolean touchEvent(TouchEvent message) {
  if (TouchEvent.CLICK == message.getEvent()) {
    int x = message.getX(1);
    int y = message.getY(1);
    XYRect myExtent;
    getExtent(myExtent);
    myExtent.translate(0, 0); // getX, getY values are relative to this field's upper-left corner
    if (!myExtent.contains(x, y)) {
      return true;
    }
  }
  return super.touchEvent(message);
}

 You might want to allow a "near-miss" to avoid user frustration - extend myExtent by whatever amount you feel like on each side.

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!