6
vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
    vfm.add(new LabelField("horizontally centered...",Field.FIELD_HCENTER | LabelField.FOCUSABLE));
    vfm.add(new LabelField("horizontally centered...",Field.FIELD_HCENTER | LabelField.USE_ALL_WIDTH | LabelField.FOCUSABLE));

    add(vfm);

Why can't I get my fields to be horizontally aligned. I have tried different combinations but can't get a single labelfield to be centered. If I add a second field below with USE_ALL_WIDTH then the first field gets centered.

I don't know what the proper way of doing it!

EDIT:

Following the link provided below, I tried doing:

vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT){


        protected void sublayout( int width, int height ) {

            super.sublayout( width, height );

            width = getWidth();
            height = getHeight();

            for (int i = 0;i < this.getFieldCount() - 1; i++)
            {
                System.out.println("field:" + i);
                Field field = this.getField(i);
                //this positions the item in the middle of the manager
                int x = (int)((width - field.getWidth()) * 0.50);
                setPositionChild(field, x, field.getTop());
            }
        }


    };


    vfm.add(new LabelField("Facebook"));


    add(vfm);

The problem is that I'm not getting any fields. How am I supposed to implement it?

Adam
  • 8,849
  • 16
  • 67
  • 131

3 Answers3

11

Here are the rules for alignment on BlackBerry:

  • A HorizontalFieldManager can only align the fields within it vertically. So when creating those fields only the following styles (also known as alignment bits) have any effect: FIELD_TOP, FIELD_VCENTER, FIELD_BOTTOM. e.g. new LabelField("My field", Field.Field_VCENTER)

HorizontalFieldManager example

HorizontalFieldManager example

  • A VerticalFieldManager can only align the fields within it horizontally. Only the following styles have any effect: FIELD_LEFT, FIELD_HCENTER, FIELD_RIGHT.

VerticalFieldManager example

enter image description here

Aligning both horizontally and vertically example

Here's an example which aligns a button in the center of the screen both vertically and horizontally:

public class AlignmentScreen extends MainScreen{

        public AlignmentScreen(){

            //A MainScreen has a VerticalFieldManager to lay out its 
            //fields from top to bottom, the style bits here tell it
            //to span the whole width of the screen and not to scroll
            super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL);

            //Set the VerticalFieldManager to have a GREEN background
            getMainManager().setBackground(BackgroundFactory.createSolidBackground(0xFF00FF00));

            //Create a new HorizontalFieldManager which is centered horizontally
            //and spans the whole height of the containing VerticalFieldManager
            HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_HCENTER | Manager.USE_ALL_HEIGHT);

            //Set the HorizontalFieldManager's background to RED
            hfm.setBackground(BackgroundFactory.createSolidBackground(0xFFFF0000));

            //Create a button and center align it vertically
            ButtonField button = new ButtonField("Test", Field.FIELD_VCENTER);

            //Add the button to the HoriztonalFieldManager
            hfm.add(button);

            //Add the HorizontalFieldManager to the VerticalFieldManager
            add(hfm);
        }
    }

The screen should look like this:

Centering vertically and horizontally

You should be able to modify the above to get your fields to align the way you want.

Nate
  • 31,017
  • 13
  • 83
  • 207
donturner
  • 17,867
  • 8
  • 59
  • 81
  • Nice One. but can i center the VerticalFieldManager as you have center the Test Button Field? My Issue is i have some Field in the Vertical Field Manager and i want to vertically center that VerticalFieldManager. Help me if you have sollutoin of it. – Shreyash Mahajan Dec 26 '12 at 12:58
  • Please don't post questions in comments, post a new question on StackOverflow. – donturner Jan 02 '13 at 10:35
  • @Adam If this answer has helped you please consider accepting it – donturner Jan 11 '14 at 14:28
2

First add your Field to a HorizontalFieldManager (say hfm), then add that hfm to the vfm, I hope this will solve your problem:

VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.FIELD_HCENTER);
hfm.add(new LabelField("My Label"));
add(hfm);

See the following link to find An effective way of aligning fields in a VerticalFieldManager

Community
  • 1
  • 1
Tariq M Nasim
  • 1,278
  • 11
  • 24
  • Thanks, but your method isn't working for me. I'll check out the link's method. I can't believe something so simple is laughably complicated – Adam Jan 03 '12 at 06:20
  • Aligning fields in BB is sometimes really complicated.BTW, On which device did you try? I've tested this on BB 9800 Torch, it seems perfect there. – Tariq M Nasim Jan 03 '12 at 06:54
  • I'm working on the 9700. I'll try and calculate within my paint method – Adam Jan 03 '12 at 15:28
2

Check this code snippet. It doesn't have to be that compicated.

public final class CenterScreen extends MainScreen {
    public CenterScreen() {        
        LabelField lbl1 = new LabelField("First and Foremost", Field.FIELD_HCENTER);
        lbl1.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.BLUE, Border.STYLE_SOLID));

        LabelField lbl2 = new LabelField("Second", Field.FIELD_HCENTER);
        lbl2.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.RED, Border.STYLE_SOLID));

        LabelField lbl3 = new LabelField("Last but not least", Field.FIELD_HCENTER);
        lbl3.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.GREEN, Border.STYLE_SOLID));

        VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);        
        vfm.add(lbl1);
        vfm.add(lbl2);
        vfm.add(lbl3);
        add(vfm);    
    }
}


Resulting in enter image description here

tonymontana
  • 5,728
  • 4
  • 34
  • 53