3

Horizontally, I want to display two bitmaps, and between them display a label field. The code seems straightforward, but all the fields are added on the left side of the screen.

HorizontalFieldManager hfm = new HorizontalFieldManager();

callbmp = new BitmapField(ei.getBitmap(),Field.FOCUSABLE |BitmapField.FIELD_LEFT);
LabelField NAME = new LabelField("mylable", LabelField.FIELD_HCENTER);
mailbmp = new BitmapField(mail.getBitmap(),Field.FOCUSABLE|BitmapField.FIELD_RIGHT);
hfm.add(callbmp);
hfm.add(NAME);
hfm.add(mailbmp);
add(hfm);
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97

3 Answers3

6
 Manager customManager = new Manager(0)
 {
     protected void sublayout(int width, int height) {
         setPositionChild(
             getField(0), 
             0, 
             0);
         layoutChild(
             getField(0), 
             getField(0).getPreferredWidth(), 
             getField(0).getPreferredHeight());

         setPositionChild(
             getField(1), 
             Graphics.getScreenWidth()/2 - getField(1).getPreferredWidth()/2, 
             0);
         layoutChild(
             getField(1), 
             getField(1).getPreferredWidth(), 
             getField(1).getPreferredHeight());    

         setPositionChild(
             getField(2), 
             Graphics.getScreenWidth() - getField(2).getPreferredWidth(), 
             0);
         layoutChild(
             getField(2), 
             getField(2).getPreferredWidth(), 
             getField(2).getPreferredHeight());    

         setExtent(width, height);
     }      
 };

 customManager.add(new BitmapField(Bitmap.getBitmapResource("image1.png")));
 customManager.add(new LabelField("Hello Alignment"));
 customManager.add(new BitmapField(Bitmap.getBitmapResource("image2.png")));
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
4

HorizontalFieldManager lays out the fields left-to-right in the order in which they are added. The style bits for horizontal layout are ignored.

If you want left, right and center on a horizontal line, you'll need a custom manager.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
  • thank u very much.. can u pls post some code how can i arrange left,right and center in custom manager – Mahesh Babu Nov 15 '10 at 12:46
  • 1
    That would be a significant investment in my time and would only hurt you - you need to learn how to do this on your own. RIM had a talk at the last devcon about advanced layout, that included a manager that does this, but I'm not able to find it right now. – Michael Donohue Nov 15 '10 at 13:28
2

This should be your requirement:
enter image description here
It can be done simply by subtracting the widths of the items you are adding on your horizontal field manager. By default the leftButton or the first item you add on the HFM will be added on left. Then you can add your label(userName) and the rightButton in the following way:

LabelField userName = new LabelField("MaheshBabu");
HorizontalFieldManager horizontalBar = new HorizontalFieldManager(USE_ALL_WIDTH|Manager.NO_HORIZONTAL_SCROLL|Manager.NO_HORIZONTAL_SCROLLBAR);
horizontalBar.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));

rightButton.setMargin(0, 0, 0, Display.getWidth()-rightButton.getPreferredWidth()-leftButton.getPreferredWidth()-userName.getPreferredWidth());

horizontalBar.add(leftButton);
horizontalBar.add(userName);
horizontalBar.add(rightButton);
Atif Imran
  • 1,899
  • 2
  • 18
  • 33