Adding fields until screen is full

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2010-02-25T15:53:51Z Indexed on 2010/03/17 13:31 UTC
Read the original article Hit count: 204

Filed under:
|

For the sake of this question, let us suppose that I want a row of buttons. I want to put as many buttons in that row as I can fit on the screen, but no more. In other words, as long as a prospective button will not be cut off or have its text shortened, add it.

It seems that I should be able to do something like:

HorizontalFieldManager hfm = new HorizontalFieldManager();

int remainingWidth = Display.getWidth();

int i =0;
while(true) {
    ButtonField bf = new ButtonField("B " + i);
    remainingWidth -= bf.getWidth();
    if(remainingWidth<0)
        break;
    hfm.add(bf);
    i++;
}

add(hfm);

But this doesn't work. bf.getWidth() is always 0. I suspect that this is because the button has not yet been laid out when I query for the width.

So, perhaps I could just make sure the buttons are always the same size. But this won't work for a few reasons:

  1. Different BB platforms have different looks for buttons and text that will fit on a button on a Curve won't fit on a button on a Storm.
  2. 3rd party themes may change the look of buttons, so I can't even count on buttons being a certain size on a certain platform.

Is there no way for me to actually check the remaining space before adding a button? It feels like a fairly useful feature; am I just missing something?

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about ui