Thursday, July 23, 2009

How to Place a Field Vertically and Horizontally center in the Blackberry Screen

For placing a Field (for example HorizontalFieldManager) in center of the screen simple setting margin will do the trick.

Here is an example of placing a HorizontalFieldManager in the center.



HorizontalFieldManager hfm = new HorizontalFieldManager();

LabelField testLabel1 = new LabelField( "Test Label1");
LabelField testLabel2 = new LabelField( "Test Label2");
hfm.add(testLabel1);
hfm.add(testLabel2);

int leftEmptySpace = (Display.getWidth() - hfm.getPreferredWidth()) / 2;
int topEmptySpace = (Display.getHeight() - hfm.getPreferredHeight()) / 2;
hfm.setMargin(topEmptySpace, 0, 0, leftEmptySpace);

//now add this manager to MainScreen
this.add(hfm);





1 comment: