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: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Center a HorizontalFieldManager vertically on the screen

Hi everyone,

 

I have a HorizontalFieldManager within a VerticalFieldManager. The HorizontalFieldManager contains a BitmapField in one "cell", and a Custom GridFieldManager in another cell.

 

I'm trying to make it so that the HorizontalFieldManager is vertically centered on the screen, but have been unable to do so. Most of the forum posts say to "create your own manager". I can do that, however I am unsure what the Manager would need to specify in order to vertically center the HorizontalFieldManager.

 

Can anyone push me in the right direction? Here is what I have so far:

 

 

imageOutputField = new BitmapField();
href = new HrefField("Synopsis", 0xffffff, 0x043184, 0x00EF3825);
href.setChangeListener(this);

grid.add(href);

VerticalFieldManager vfm = new VerticalFieldManager(
	Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT
 	| VerticalFieldManager.VERTICAL_SCROLL
	| VerticalFieldManager.VERTICAL_SCROLLBAR);

horiz = new HorizontalFieldManager(DrawStyle.VCENTER);

horiz.add(imageOutputField);
horiz.add(grid);
				
vfm.add(horiz);

synchronized (UiApplication.getEventLock()) {
	add(horiz);
}

 Thanks!

 

Developer
Posts: 74
Registered: ‎01-03-2010
My Device: Not Specified

Re: Center a HorizontalFieldManager vertically on the screen

drawstyle.VCENTER may be for text positioning,  try Field.FIELD_VCENTER

Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Center a HorizontalFieldManager vertically on the screen

Hi Jack,

 

I tried out your suggestion, and I am still receiving the same response. Does anyone have any other ideas? Thanks!

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Center a HorizontalFieldManager vertically on the screen

This works for me:

 

public class TestScreen extends MainScreen {
    public TestScreen() {
        super(NO_VERTICAL_SCROLL);
        HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_HEIGHT);
        LabelField lf = new LabelField("Sample Text", FIELD_VCENTER);
        hfm.add(lf);
        add(hfm);
    }
}

 

The key to using FIELD_VCENTER is to make sure that the parent HorizontalFieldManager is the right height. You have to be careful, especially when using USE_ALL_HEIGHT, that it doesn't get bigger than. That's why I turned off vertical scrolling for the screen. (Otherwise the text will end up centered many thousands of pixels below the screen.) As an alternative, you could subclass HorizontalFieldManager and return a preferred height of the screen height (perhaps minus a header).




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 22
Registered: ‎04-09-2010
My Device: Storm
My Carrier: Just developing

Re: Center a HorizontalFieldManager vertically on the screen

[ Edited ]

Here's another way:

http://supportforums.blackberry.com/t5/Java-Development/Vertical-center-a-field/m-p/254038

 

Bikas wrote:

 

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);
        
        this.add(hfm);  

 

Worked perfectly. In my opinion it is better this way. You create what you want inside a manager, then add the manager and after that do the math (with getPreferredWidth/height) and set the Margins.