Wednesday, February 22, 2012

Set LeftField and Right Filed

There are very common there are need to set one labelField on left side and Button or DropDownField on right side of the screen. Today i think this will help full for developed sample HoeizonalFiedlManager example that set Field Rightside and Left side.

Following code will help full.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class HFMLeftFieldRightField extends HorizontalFieldManager {

 private Field leftField;
 private Field rightField;
 
 private final static int TOP_MARGIN = 0;
 private final static int LEFT_MARGIN = 30;

 public HFMLeftFieldRightField() {
  super(USE_ALL_WIDTH);
 }

 public HFMLeftFieldRightField(boolean isQatari) {
  super(USE_ALL_WIDTH | Field.FIELD_LEFT);
 }

 public void sublayout(int maxWidth, int maxHeight) {

  super.sublayout(maxWidth, maxHeight);
  int width = getWidth();

  if (rightField != null) {
   int x = width - rightField.getWidth() - LEFT_MARGIN;
   int y = TOP_MARGIN;
   setPositionChild(rightField, x, y);
  }
  if (leftField != null) {
   int y = TOP_MARGIN+rightField.getHeight()/5;
   int x = LEFT_MARGIN;
   setPositionChild(leftField, 0, y);
  }
 
  setExtent(maxWidth, rightField.getHeight() + TOP_MARGIN * 2);
 }

 public void setLeftButton(Field leftField) {
  this.leftField = leftField;
  super.add(leftField);
 }

 public void setRightButton(Field rightField) {
  this.rightField = rightField;
  super.add(rightField);
 }


}

Following code add on MainScreen where you want to add Filed.
HFMLeftFieldRightField hfm = new HFMLeftFieldRightField();
    hfm.setLeftButton(new LabelField("Select Country"));
    ObjectChoiceField objChoField = new ObjectChoiceField();
    String[] str = {"India","Germany","Australia","Canada"};
    objChoField.setChoices(str);
    hfm.setRightButton(objChoField);
 
    Bitmap borderBitmap = Bitmap.getBitmapResource("img/rounded-border.png");
    hfm.setBorder(BorderFactory.createBitmapBorder(new XYEdges(12,12,12,12), borderBitmap));
    add(hfm);


Output will like this.


No comments:

Post a Comment