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
Posts: 121
Registered: ‎11-19-2008
My Device: Not Specified
Accepted Solution

Touchscreen button that doesn't hold the focus?

I am trying to implement a touchscreen navigation panel very similar to what the native Calendar, Email and Tasks applications do on the Torch or Storm. I am positioning a series of image buttons (actual class derives from BitmapField) at the bottom of the screen to provide visual touchscreen buttons for common operations on the items on the list (e.g. New, Delete, etc).

 

However, when clicking on those "buttons" they take the focus from the rest of the screen and I don't want them to. The rest of the screen is a ListField and I want the list to maintain it's current selection. I currently get around with an ugly hack and call ListField.setFocus() whenever the button click is detected, but this adds an unpleasant flicker to the screen as focus moves from the list to the button and back to the list in short succession.

 

Any help would be greatly appreciated.

New Contributor
Posts: 8
Registered: ‎08-23-2010
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

You can override the isFocusable() of your Custom ButtonField and trigger fieldChangeNotify(0) for touch event.

 

 

public boolean isFocusable() {
return false;
}

 

But if your field is not focusable then you can't select this field with trackball / trackpad.

Developer
Posts: 121
Registered: ‎11-19-2008
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

Thanks for the response! Returning false in isFocusable() and processing the touch event did succeed in not stealing the focus.

 

However, the buttons now do not show a different state when pressed so the user doesn't get a visual indication that he pressed the button.Would there be any way to get around this, short of doing everything from scratch like repainting the background on touch down and up?

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

Re: Touchscreen button that doesn't hold the focus?

You can try using setVisualState on your buttons - VISUAL_STATE_ACTIVE in TouchEvent.DOWN processing and VISUAL_STATE_NORMAL (plus invoking the action) in TouchEvent.UP case.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 121
Registered: ‎11-19-2008
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

This does work, but only to a certain extent. Because the buttons are positioned inside a HorizontalFieldManager, any touch event inside the manager triggers a TouchEvent.UP/DOWN event in the individual buttons. So I end up with a button that looks pressed even if the click was far far away at the opposite end of the control.

 

Is there a manager that intelligently maps the TouchEvent.UP and TouchEvent.DOWN to the appropriate field? Alternatively, how can I calculate  and map the touch event coordinates to the right button?

New Contributor
Posts: 8
Registered: ‎08-23-2010
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

When you override the touchEvent(..) of your Custom Field, just check if the touch point is in your desired region.

 

 

protected boolean touchEvent(TouchEvent message) {
        int touchX= message.getX(1);
        int touchY = message.getY(1);
        XYRect rect = getExtent();
        if (rect.contains(touchX, touchY)) {
            // do something here..
            return true;
        }

        return super.touchEvent(message); }

 

If it doesn't work then you may have to implement touchEvent(..) of the parent field manager.

 

protected boolean touchEvent(TouchEvent message)
{
    // kill the click events that happen outside any fields
    if (getFieldAtLocation(message.getX(1), message.getY(1)) < 0)
        return true;
    return super.touchEvent(message);
}

// The regular getFieldAtLocation returns wrong values in  
// open spaces in complex managers, so need to override it
public int getFieldAtLocation(int x, int y)
{
    XYRect rect = new XYRect();
    int index = getFieldCount() - 1;
    while (index >= 0)
    {
        getField(index).getExtent(rect);
        if (rect.contains(x, y)) break;
        --index;
    }
    return index;
}

 

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

Re: Touchscreen button that doesn't hold the focus?


Rrupak wrote:

When you override the touchEvent(..) of your Custom Field, just check if the touch point is in your desired region.

 

 

protected boolean touchEvent(TouchEvent message) {
        int touchX= message.getX(1);
        int touchY = message.getY(1);
        XYRect rect = getExtent();
        if (rect.contains(touchX, touchY)) {
            // do something here..
            return true;
        }

        return super.touchEvent(message); }

 

 

This check (if getExtent().contains(touchX, touchY)) is wrong - the proper way is along the lines of

if (touchX >= 0 && touchX < getWidth() && touchY >= 0 && touchY < getHeight()) {
  ...
}

 getX and getY return values 'local' to the field/manager where touchEvent is overridden, while getExtent returns field's position relative to its manager. Hence the difference.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
New Contributor
Posts: 8
Registered: ‎08-23-2010
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

Thankyou arkadyz for your correction. I didn't know that there was message.getX(1) instead of message.getGlobalX(1). Thankyou again.

 

protected boolean touchEvent(TouchEvent message) {		
int touchX = message.getGlobalX(1);
int touchY = message.getGlobalY(1);
XYRect rect = getExtent();
if (rect.contains(touchX, touchY)) {
// do something here..
return true;
}

return super.touchEvent(message);
}

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Touchscreen button that doesn't hold the focus?

Developer
Posts: 263
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Touchscreen button that doesn't hold the focus?

You guys are very clever. But my solution for a list field to maintain it's current selection (e.g., mantian the blue background of the current selected item) when it has lost the focus is as follows:

 

...
boolean isListFieldFocus = true;
...
listField = new ListField(){
  protected void onUnfocus() {
    isListFieldFocus = false;
  };
 protected void onFocus(int direction) {
  isListFieldFocus = true;
 };
};
...
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
 if (index > -1 && index < listField.getSize()) {
  if(isListFieldFocus || index != listField.getSelectedIndex()){
   //draw the row normally, for example
   //g.drawText("blabla", 4, y + 2, 0, width);
  }else{//the list field is lost the focus
   //draw the row differently. e.g., set the background, change font...
   //g.drawText("blabla...", 4, y + 2, 0, width);
  }
 }
}

 

--tom