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: 91
Registered: ‎02-04-2009
My Device: Not Specified
Accepted Solution

ButtonField Focus in 4.6.0

In 4.6.0 on a Bold and a Pearl Flip, my ButtonFields are not drawn correctly in some situations.  This happens in the simulator and on real devices, but only with 4.6.0.  4.6.1 on an 8900 is fine, as are 4.2.1, 4.5, and 4.7.0 phones.

 

When my application starts, it pushes a MainScreen with two ButtonFields.  Clicking either button will result in a new MainScreen getting pushed.  When that second screen is poped and the first is displayed again, the button that was clicked still has focus.  However, the focused button is not painted correctly.  It should be blue with white text, but it is dark grey with white text.  The other button, which is not focused, is dark grey with light grey text.  So, the focused button has the correct text color, but the wrong background color.  These are all standard widgets and I'm not doing any custom coloring.

 

If I quickly press and release the End key in this state, the button will repaint with the right coloring just before my application goes to the background.  When I relaunch the application, the buttons are painted correctly.

 

If I get back to that incorrect state and then press and hold the End key, the application goes to the background but does not trigger the repaint.  When I relaunch, the buttons will still be painted incorrectly.

 

What is the right way to work around this problem?  I've tried calling setFocus, focusRemove/focusAdd, and invalidate from the onExposed methods of the ButtonField, but none of those fixed the problem.  Any suggestions?

Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: ButtonField Focus in 4.6.0

Here is the solution I came up with:

 

protected void onExposed() {

super.onExposed();

 

Field field = getLeafFieldWithFocus();

removeFocus();

if (field == null)

setFocus();

else

field.setFocus();

}

 

 It seems to resolve the issue on 4.6.0 without causing problems on other versions.

 

Developer
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold
My Carrier: Telefonica

Re: ButtonField Focus in 4.6.0

Hey. Thanks.

New Developer
Posts: 12
Registered: ‎08-12-2009
My Device: Not Specified

Re: ButtonField Focus in 4.6.0

[ Edited ]

Your solution works great unless you are overriding the onFocus and onUnfocus methods in the button to run custom code because those methods will be called twice.

 

The simple work around I finally found was to call setVisualState(VISUAL_STATE_FOCUS) inside the button's onExposed method.  This would prevent the onFocus and onUnfocus from being called twice.

 

 

/*
*inside buttonfield
*/
protected void onExposed() {
      super.onExposed();
      /*
       * if this button has focus, make sure it draws properly.
       */
      if(isFocus()){
         setVisualState(VISUAL_STATE_FOCUS);
      }
}

 

 

Simple but effective.

Highlighted
Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: ButtonField Focus in 4.6.0

I'm not doing BlackBerry development anymore, so I can't confirm that fix.  However, I did want to point out that setVisualState(int) was added to the API in 4.6.0.  The application I was writing had to run on 4.2.1 devices, so it wouldn't have worked for me.  Thanks for posting your fix though; I'm sure someone will be able to use it.

 

It's hard to believe I originally asked this question a year ago.  Time flies!