Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
dhanashreerao
Posts: 51
Registered: ‎08-25-2008
My Device: Not Specified
Accepted Solution

how to calculate height of LabelField when it displays in multi lines

Hello Friends,

 

We use LabelField.getPrefereredHeight() for calculating height of Label field. But it returns same value when it's displayed in either singel line or multi line.

 

Please help me for differing the height of label field while displaying in single line and multiline

 

Thanks lot in advance

Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: how to calculate height of LabelField when it displays in multi lines

Field.getHeight()?
Please use plain text.
Developer
dhanashreerao
Posts: 51
Registered: ‎08-25-2008
My Device: Not Specified

Re: how to calculate height of LabelField when it displays in multi lines

Good afternoon

My code is so simple. I have taken 1 verticalFieldManager, setting it's sublayout with a fixed width and height as width1 and height1. And I want to add a lable at exactly vertically centre to it.

 

In layout method of LabelField, we can set a position of that field as

setPosition(x,y)

 

In my code

x  =  0;

y =  (height1 - labelField.getHeight() ) / 2;

 

but labelField's height is always returning 0 and labelField.getPrefereredHeight() is returning same value when label is in sigle line or in multiline.

 

So how should I set it's position?

 

Thanking in advance

 

 

 

 

Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: how to calculate height of LabelField when it displays in multi lines

If the code is simple then you might want to go ahead and post it.  I can't really conjecture on where exactly the problem lies in this case unless I see code.  It does sound like you should not be setting the width and height of your manager to 1 though.  Here's a sample of a sublayout from one of my managers:

 

protected void sublayout(int width, int height) { int numFields = getFieldCount();

int availableHeight = height; int totalHeight = 0; for (int i = 0; i < numFields; i++) { Field field = getField(i); layoutChild(field, width, availableHeight);

availableHeight -= field.getHeight; totalHeight += field.getHeight(); } setExtent(width, totalHeight); }

 

  This code is instructing the manager to use all of the width provided to it and only use as much height as it needs (bu laying out its children and adding up their respective heights).  I don't know if any of this helps, but if you have more questions please feel free to post code and we'll see what's up.
Please use plain text.
Developer
gercoder
Posts: 46
Registered: ‎06-11-2009
My Device: Bold 2 - Pearl 8120

Re: how to calculate height of LabelField when it displays in multi lines

This is exactlly was I looking for, to solve a layout problem with multilines labels.

 

Thanks very much.

 

 

If your issue is resolved, please mark this thread as solved. Click "Accept as solution" on the post that contains the answer to your question.
Please use plain text.
Contributor
bogdannistor
Posts: 14
Registered: ‎01-20-2011
My Device: BlackBerry Bold 9700

Re: how to calculate height of LabelField when it displays in multi lines

Or, simpler, you could use:

 

(getFont().getAdvance(yourString) / fieldWidth) * getFont().getHeight()

 

Source: http://stackoverflow.com/a/8001155/219321 (Farid Farhat's comment)

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to calculate height of LabelField when it displays in multi lines

Code posted above does not take line breaks or word wrapping into account.  Go with the posted solution which does. 

Please use plain text.