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
Contributor
yoshiegg
Posts: 22
Registered: ‎06-02-2010
My Device: 8330
My Carrier: Rogers & Bell
Accepted Solution

Manager Madness!

I want to make a pair of items on a single horizontal row. I want the label to be on the left side of the screen and the button to be on the right side with the white space between them. However the code below doesn't appear to be working and it's ignoring the left and right field styles.

 

 

        HorizontalFieldManager pOptionPairManager = new HorizontalFieldManager();
pOptionPairManager.add(new LabelField("test label", Field.FIELD_LEFT));
pOptionPairManager.add(new ButtonField("text button", Field.FIELD_RIGHT));
add(pOptionPairManager)

 

 

 

instead the label shows up on the left side (like it should) and the button is squished to the right side of the label with the space between the button and the side of the screen.

 

How do I fix this?

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Manager Madness!

HorizontalFieldManager ignores all horizontal alignment styles, so FIELD_RIGHT, FIELD_LEFT and FIELD_HCENTER have zero effect on fields added to it.

 

You will have to use a custom layout Manager - something like this:

 

public class MyOptionPairManager extends Manager {
  protected void sublayout(int maxWidth, int maxHeight) {
    Field field1 = getField(0);
    Field field2 = getField(1);
    int height;
    int availableWidth;

    layoutChild(field1, maxWidth, maxHeight);
    availableWidth = maxWidth - field1.getWidth();
    layoutChild(field2, availableWidth, maxHeight);
    height = Math.max(field1.getHeight(), field2.getHeight());
    setPositionChild(field1, 0, 0);
    setPositionChild(field2, maxWidth - field2.getWidth(), 0);
    setExtent(maxWidth, height);
  }
}

If you want to, you can calculate the y coordinate in setPositionChild() to vertically center the fields (something along the lines of

 

int y = 0;
if (field1.isStyle(FIELD_VCENTER)) {
  y = (height - field1.getHeight()) / 2;
}
setPositionChild(field1, 0, y);
...

and the same for field2).

 

Having created this class, add your label and button to this custom manager and get the result you wanted.

 

Obviously, it can handle only two fields within it. If you want several evenly spaced fields, take a look at EvenlySpacedHorizontalFieldManager in

Implement advanced buttons, fields, and managers

That article is an incredible resource of all UI things custom - don't just download and use it, take a look into the code and learn!

 

 

 

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