2


I wanna center a buttonField in a HorizontalFieldManager
i tried that code :

   HorizontalFieldManager ButM = new HorizontalFieldManager(Field.FIELD_HCENTER|USE_ALL_WIDTH)
    {
        public void paint(Graphics graphics)
        {
            graphics.setBackgroundColor(0x00000000);
            graphics.clear();
            super.paint(graphics);
        }  
    };
    ButtonField Order = new ButtonField("Tri",Field.FIELD_HCENTER|ButtonField.CONSUME_CLICK);

But the ButtonField is not centred, am i doing something wrong ?

EDIT :
I solved this problem by using this code :

HorizontalFieldManager ButM = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER|USE_ALL_WIDTH)
    {
      public void paint(Graphics graphics)
      {
          graphics.setBackgroundColor(0x00000000);
          graphics.clear();
          super.paint(graphics);
      }  
    };
    ButtonField Order = new ButtonField("Tri",DrawStyle.HCENTER|ButtonField.CONSUME_CLICK);
    Order.setMargin(0,0,0,(Display.getWidth()/2)-30);
Mehdi
  • 966
  • 1
  • 9
  • 24

3 Answers3

1

Try this bit of code

HorizontalFieldManager ButM = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER)
 {
  public void paint(Graphics graphics)
    {
      graphics.setBackgroundColor(0x00000000);
     graphics.clear();
     super.paint(graphics);
  }  
  };
  ButtonField Order = new ButtonField("Tri",DrawStyle.HCENTER|ButtonField.CONSUME_CLICK);
  ButM.add(Order);
rfsk2010
  • 8,271
  • 4
  • 28
  • 43
  • I tried to remove USE_ALL_WIDTH allready, but when i do this, the background of the HorizontalFieldManager is black only near the button, and the rest of the screen remain white – Mehdi Dec 28 '11 at 12:25
1
HorizontalFieldManager ButM = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER)
{
  public void paint(Graphics graphics)
  {
      graphics.setBackgroundColor(0x00000000);
      graphics.clear();
      super.paint(graphics);
  }  
};
ButtonField Order = new ButtonField("Tri",DrawStyle.HCENTER|ButtonField.CONSUME_CLICK);
Order.setMargin(0,0,0,(Display.getWidth() - Order.getPreferredWidth())/2 );
ButM.add(Order);

This code will run fine.. I have test it...

V.J.
  • 8,872
  • 4
  • 31
  • 49
  • thanks, I added USE_ALL_WIDTH to my Horizontalfieldmanager then modified the setMargin Method to make it work, thanks again – Mehdi Dec 29 '11 at 09:31
0
HorizontalFieldManager ButM = new HorizontalFieldManager(USE_ALL_WIDTH)
{
    public void paint(Graphics graphics)
    {
        graphics.setBackgroundColor(0x00000000);
        graphics.clear();
        super.paint(graphics);
    }  
    public void sublayout(int width,int height)
    {
      layoutChild(button,preferredWidth,preferredHeight);
      setPositionChild(button, (Graphics.getScreenWidth()-button.getWidth())/2, 0);
      setExtent(Graphics.getScreenWidth(),this.getHeight());
    }
};

try this.. hope it will help you

Swati
  • 1,169
  • 9
  • 28