mostruash
mostruash

Reputation: 4189

Set height of a ButtonField

I need to change the size of a ButtonField. It is inside a VerticalFieldManager which is the only VFM in the MainPage.

I have already changed its width successfully (overriding setPreferredWidth() was enough). However I'm having trouble with changing its height.

Until now, I overrode setPreferredHeight() and it did not do anything. I've tried overriding layout like this (tried all permutations of commented rows):

protected void layout(int arg0, int arg1)
{
    //super.layout(myDesiredWidth, myDesiredHeight);
    //setExtent(myDesiredWidth, myDesiredHeight);
}

I have also tried overriding sublayout of its manager (the vfm) like this:

protected void sublayout(int arg0, int arg1)
{
    super.sublayout(arg0, arg1);
    ButtonField myButton = (ButtonField)getField(1);
    layoutChild(myButton,myDesiredWidth,myDesiredHeight);
}

It didn't work. I am still not sure how those layout and sublayout methods work in the background but I'm sure that someone needed to alter the size of a button before me.

Edit: I'll be more specific. I was using BB JRE 6.0 and overriding getPreferredHeight() and layout() was buggy (changing height would alter width tremendously, other fields around my button would lose their text etc.). I tried BB JRE 5.0 and increasing the height worked but decreasing didn't work. Whatever, I googled a lot and I couldn't find a clear answer, I guess I have to create my own custom Field and implement paint method from scratch.

Upvotes: 0

Views: 898

Answers (3)

Hesham
Hesham

Reputation: 5274

Use a customizable ButtonField like this one: https://github.com/HeshamMegid/BlackBerry-Custom-Controls

Upvotes: 0

rfsk2010
rfsk2010

Reputation: 8611

Blackberry screen works in the following way.

  1. The delegate manager of a screen lays out itself.
  2. The delegate manager asks the other fields and managers to lay out.
  3. The managers added to the delegate manager lays out itself and asks its child fields or managers to layout.
  4. This process continues until all the fields are laid out.

You want to layout a button field which is part of a manager. Based on the rules above, you have to override the layout method of the button field only. As alishaik786 said, you dont have to layout the manager or anything else other than the buttonField.

A simple example of a custom button field is given here at http://www.coderholic.com/blackberry-custom-button-field/

If you look at the code you can see the following overrided methods which are necessary for laying out the field itself

public int getPreferredWidth()
{
    return fieldWidth;
}

public int getPreferredHeight()
{
    return fieldHeight;
}

protected void layout(int arg0, int arg1)
{
    setExtent(getPreferredWidth(), getPreferredHeight());
}

Once you have set the fieldWidth and fieldHeight in the constructor, you can return those values for getPreferredWidth() and getPreferredHeight() respectively.

the layout() of the field is called, when the field is being laid out which will set the extent of the field to fieldWidth and fieldHeight

Upvotes: 1

alishaik786
alishaik786

Reputation: 3726

see this sample Class:

public class Abc extends MainScreen
{   
ButtonField clickButton;
public Abc()
{   
    createGUI();    
}
public void createGUI() 
{
    clickButton=new ButtonField("Click Here", Field.FIELD_HCENTER)
    {
        protected void layout(int width, int height) 
        {
            setExtent(200, 80);
        }
    };
    add(clickButton);                   
}
}

I get like this image:

Size of button

Use according to your requirement;

Upvotes: 1

Related Questions