how to calculate Labelfield and HorizontalFieldmanager height in blackberry

866 views Asked by At

I have HorizontalFieldManager,VerticalFieldManager and LabelField. LabelField placed in HorizontalFieldManager and multiple HorizontalFieldManager are placed in VerticalFieldManager.

When i try to get LabelField height using labelfield.getHeight(); it returns 0 . if there are multiple line in Labelfield, it also give me height returns 0. same issue i m facing for HorizontalFieldManager .

After getting there height i want to calculate VerticalFieldManager height and set height dynamically for the screen.

How can i calculate the height of Label or Horizontalfieldmanager?

3

There are 3 answers

5
Farid Farhat On

Use labelField.getPreferredHeight() and manager.getPreferredHeight() not labelField.getHeight()

This should work for you

3
AAverin On

The method Farid suggests it's a bit difficult to use, because you will need labelWidth. For multiline label this labelWidth may not be the same as parent managers available width, or some exact width you had set, because each line can have different width depending on if words did fit maxWidth or didn't.

NOTE: as Nate pointed out in comments, it's a good idea to also add advance for spaces. I modified the code to include that.

Here is the method I use to overcome these problems:

public int getPreferredHeight() {
        String text = getText();

        int maxWidth = getManager().getPreferredWidth();
        int spaceAdvance = getFont().getAdvance(' ');
        String[] words = StringUtilities.stringToWords(text);
        int lastWordAdvance = 0;
        int lines = 1;
        for (int i = 0; i < words.length; i++) {
            int wordAdvance = getFont().getAdvance(words[i]) + spaceAdvance;
            if (lastWordAdvance + wordAdvance < maxWidth ) {
                lastWordAdvance += wordAdvance;
            } else {
                lines++;
                lastWordAdvance = wordAdvance;
            }
        }
        return (int)lines * getFont().getHeight(); 
    }
0
Peter Strange On

If you can leave the decision about the size of the LabelField until after it has been laid out, then you will get it accurately from getHeight(). This means you can actually get the correct result, including factoring in any margin or padding for the LabelField, that I think

int maxWidth = getManager().getPreferredWidth();

will miss.

Initially this seems quite difficult, because typically it is good to know the height when you add the Field to the screen. But the general principle is that you should do this in the Manager's sublayout, so you are just moving the code that is dependent on the height, a bit later in the process. And this has the benefit that the layout is dynamic, so if the LabelField's text is changed, then layout will be invoked and your code that is dependent on the height gets re-invoked too.

It is also possible to use logic like this in sublayout():

super.sublayout(...);

if (myField.getHeight() < 100 ) {

  myField.setMargin((100 - myField.getHeight())/2, 0, (100 - myField.getHeight())/2, 0);

  super.sublayout(...);

}

This is not a production suitable example, hard coding a pixel height is not recommended. It is just an easy example to understand....