1

I want to center a logo and a textfield in the middle of the screen, both vertically and horizontally. What is the best way to achieve this?

i have been trying with this so far..

public upd8rMainScreen(){

    Bitmap logoBitmap = Bitmap.getBitmapResource("upd8rLOGO.png");
    bitmapField = new BitmapField(logoBitmap, Field.FIELD_VCENTER);
    labelfield = new LabelField("Tap Your Phone Onto The Reader To Activate",Field.FIELD_VCENTER);


    topHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
    middleHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);

    vfm = new VerticalFieldManager();
    topHfm.add(bitmapField);
    middleHfm.add(labelfield);

    vfm.add(topHfm);
    vfm.add(middleHfm);
    add(vfm);

}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Chris Mccabe
  • 1,902
  • 6
  • 30
  • 61
  • 1
    Try adding the style bit **`Manager.FIELD_HCENTER`** along with the **`Manager.VERTICAL_SCROLL`** style bit, i.e use **`new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.FIELD_HCENTER)`**. See an answer I've posted earlier this morning [here](http://stackoverflow.com/questions/8707608/horizontally-centering-fields-in-a-vertical-field-manager), this may help you. – Tariq M Nasim Jan 03 '12 at 11:32

2 Answers2

4

May be it help you Try this

Bitmap logoBitmap = Bitmap.getBitmapResource("basket.png");
        bitmapField = new BitmapField(logoBitmap,Field.FIELD_HCENTER);
        labelfield = new LabelField("Tap Your Phone Onto ",Field.FIELD_HCENTER);
//     
        VerticalFieldManager vrt=new VerticalFieldManager(USE_ALL_WIDTH)
        {
            protected void sublayout(int maxWidth, int maxHeight) {

                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }
        };
        Font f=labelfield.getFont();
        int hight1=f.getAdvance(labelfield.getText());
        int k=labelfield.getPreferredHeight();

        int number=hight1/Display.getWidth()+1;
        int hight2=logoBitmap.getHeight();
        int padding=(Display.getHeight()-((number*k)+hight2))/2;
        if(padding>0){
            bitmapField.setPadding(padding,0,0,0);
        }

        vrt.add(bitmapField);
        vrt.add(labelfield);

image display like following

enter image description here

Govindarao Kondala
  • 2,862
  • 17
  • 27
0

You can try re-setting the margin of the VerticalFieldManager like this :

topHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
midHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);

topHfm.add(bitmapField);
midHfm.add(new LabelField("My Label", Manager.FIELD_HCENTER));

VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
vfm.add(topHfm);
vfm.add(midHfm);

int screenHeight = Display.getHeight();
int screenWidth = Display.getWidth();

int totalContentHeight = topHfm.getPreferredHeight()+ midHfm.getPreferredHeight();
int maxContentWidth = (topHfm.getPreferredWidth()>midHfm.getPreferredWidth()) ? topHfm.getPreferredWidth() : midHfm.getPreferredWidth() ;

int marginTop = (screenHeight>totalContentHeight)? (screenHeight - totalContentHeight) / 2 : 0;
int marginLeft = (screenWidth>maxContentWidth)? (screenWidth - maxContentWidth ) / 2 : 0;

vfm.setMargin(marginTop, 0, 0, marginLeft);

add(vfm);

I don't claim this is the "best way" to do this, but I hope it'll work for your case.

Tariq M Nasim
  • 1,278
  • 11
  • 24