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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

center ButtonField inside HorizontalFieldManager - how to ?

Hello.

 

I have a hard time to accomplish very, very simple task - horizontally center Button inside HorizontalFieldManager.

It's probably something very simple - like setting different style - but I cannot manage to find the solution.

 

Current code: 

 

HorizontalFieldManager hman = new HorizontalFieldManager(Field.USE_ALL_WIDTH)
{
  public void paint(Graphics g)
  {
    g.setColor(Color.GREEN);
    g.fillRect(0, 0, 1000, 1000);
    super.paint(g);
  }
};

ButtonField button = new ButtonField("A Button", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER | ButtonField.FIELD_VCENTER | DrawStyle.HCENTER | ButtonField.NEVER_DIRTY | Field.USE_ALL_WIDTH)
{
  public void paint(Graphics g)
  {
    g.setColor(Color.RED);
    g.fillRect(0, 0, 1000, 1000);
//        super.paint(g);
  }
};
hman.add(button);

add(hman);

And here is the result: 

 

non_centered_button.png

 

 I'd like to see the button in the center of the green area. 

 

Any help is appreciated - thanks in advance !

Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

i try below code in bb jde4.6 ,

it fulfil ur requirement .

 

code:

 

HorizontalFieldManager hman = new HorizontalFieldManager(Field.USE_ALL_WIDTH|HorizontalFieldManager.FIELD_HCENTER)
        {
                public void paint(Graphics g)
                {
                    g.setColor(Color.GREEN);
                    g.fillRect(0, 0, 1000, 1000);
                    super.paint(g);
                }
                protected void sublayout(int width, int height)
               {
                    int imageWidth = button.getPreferredWidth();
                    int imageHeight = button.getPreferredHeight();
                    layoutChild(button,imageWidth, imageHeight);
                    setPositionChild(button,(Display.getWidth()/2-35),0);
                    setExtent(width, height);
               }
        };

 button = new ButtonField("A Button", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER | ButtonField.FIELD_VCENTER | DrawStyle.HCENTER | ButtonField.NEVER_DIRTY | Field.USE_ALL_WIDTH)
{
  public void paint(Graphics g)
  {
    g.setColor(Color.RED);
    g.fillRect(0, 0, 1000, 1000);
//        super.paint(g);
  }
};
hman.add(button);

add(hman);

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

Try removing the Field.USE_ALL_WIDTH flag. I'm guessing this was just part of you trying to get it to work, but you don't need the DrawStyle.HCENTER flag either.

Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

> it fulfil ur requirement .

 

In my actual code, there are some other components inside the main HorizontalManager and I want to center the Button in the free space.

 

I'd rather not implement my own layout management - unless absolutely necessary.

Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

> Try removing the Field.USE_ALL_WIDTH flag.
> ...
> but you don't need the DrawStyle.HCENTER flag either.

Removing those two flags from Button constructor does not solve the problem.

ButtonField button = new ButtonField("A Button", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER | ButtonField.FIELD_VCENTER | ButtonField.NEVER_DIRTY)
Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

I was able to make the button centered (i.e. center it inside the "free horizontal space") by wrapping it in  VerticalFieldManager and HorizontalFieldManager - which seems a lot of overhead for such simple task.  

 

// replace 
hman.add(button);

// with 
VerticalFieldManager bhman = new VerticalFieldManager(Field.USE_ALL_WIDTH);
HorizontalFieldManager bhman2 = new HorizontalFieldManager(Field.FIELD_HCENTER);
bhman2.add(button);
bhman.add(bhman2);
hman.add(bhman);

 

Tomorrow I will spend another half an hour looking for simpler solution, meanwhile any ideas are welcome.

Trusted Contributor
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

 Hey ,

 

Do u try code which I post above? 

Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

> Do u try code which I post above?

Yes, I already responded in an earlier comment.

> I'd rather not implement my own layout management - unless absolutely necessary.
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: center ButtonField inside HorizontalFieldManager - how to ?

implementing an own layout is the easiest and fastest solution
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 224
Registered: ‎08-11-2010
My Device: Not Specified

Re: center ButtonField inside HorizontalFieldManager - how to ?

> implementing an own layout is the easiest and fastest solution

 

IMO, putting the button inside two containers is easier and less error prone ...

 

Anyway, I was looking for solution which involves some style bits, if such does not exist (though I do not understand why), using two more containers is acceptable.