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
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

How to do Text Wrapping for text in List Field with Checkboxes ?

[ Edited ]

Hi all,

 

I m creating a ListField with Checkboxex from  How To - Create a ListField with check boxes

 

But I m not getting how to wrap Text in a row in that ListField.

 

I referred the code http://pastie.org/282759/wrap but the Checkbox is not coming in ListField.

 

I think we cant mess up the code in these 2 links.

 

I referred the thread

 

Text Wrapping for text in List Field items

 

Here its written as

 

If all you are displaying is text, then I would go with the approach suggested by rab.

 I m displaying Text only in ListField which I want to wrap if it doesnt fit within device screen.

 

But i m not getting "approach suggested by rab"  in which How to calculate?

 

 String [] linesToDraw = calculate the number of lines depending on the row width

 

I m stucked at this so any details on this issue ?

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

You calculate the width of the text using Font.getAdvance(String text)

 

See the API docs.

 

Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

I got the no. of lines properly according to row width & stored it in a Vector & i m doing

 

 

        int fontHeight = this.getFont().getHeight();

         for (int i = 0; i < linesToDraw .size(); i++)
        {           
            textToDraw = (String)linesToDraw.elementAt(i);
            graphics.drawText(textToDraw, xpos, ypos, 0 , width);
            ypos += fontHeight;
        }

now its drawing the lines but over the previous one as by default listField.setRowHeight() is taking only one row. If there r 2 rows then I write setRowHeight(getFont().getHeight()*2) in drawListRow() but then it goes in loop

 

 

If I write setRowHeight() while creating ListField then it comes properly but for all rows same row height is set.

 

I gets the String to draw at Runtime & the row height should be variable for different rows then how can I setRowHeight() with what parameter & in which method ?

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

There is no public API to have a variable height ListField.

 

There are a number of Threads on this forum that discuss this and the various options, search round for them. 

New Developer
Posts: 17
Registered: ‎12-02-2009
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

Hey Shreyas can you guide me to calculate number of lines required? and also to create linesToDraw collection?

 

Thanks in Advance

Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

The Logic is like..

 

 

    * split text on words
* create string array and while words array not empty do
o create string lineStr
o while getFont().getAdvance(lineStr+" "+words[i]) < row width move words[i] to lineStr

in the end you will get:

* String[] lines with lines for row
* Row height = lines.lenght * (getFont().getHeight() + topMargin + bottomMargin)

& do g.drawText() like in my previous post.

 

But i m confused from where to call setRowHeight()  as the Row Height is varible for different Rows.

 

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

[ Edited ]

setRowHeight method apply on your ListField.

 

http://supportforums.blackberry.com/t5/Java-Development/setRowHeight-dramaticly-slows-down-performan...


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

Thanks for thr reply but  list.setRowHeight(45);   is hardcoded there

 

& In my case the row height is variable for different rows & I gets the String to draw at Runtime.

 

So if I write setRowHeight(fontHeight*2) in drawListRow() like

 

 

public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) 
{
................................
...............................
for (int i = 0; i < linesToDraw.size(); i++) { String textToDraw = (String)linesToDraw.elementAt(i); list.setRowHeight(fontHeight); graphics.drawText(textToDraw, 0, y, 0, w); y += fontHeight; }
}

 

but then it goes in loop

 

If I write setRowHeight() while creating ListField then it comes properly but for all rows same row height is set & I have t o give a fixed value there.

 

So how to write setRowHeight() with variable value & where to write it?

 

Highlighted
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to do Text Wrapping for text in List Field with Checkboxes ?

The code which provided will be just for reference and you may need to change a bit which suites your requirement.

 

Following one thread is also about setting variable height in Listfield and it is discussed about the performace too.. you just give a look at it.

 

http://supportforums.blackberry.com/t5/Java-Development/Variable-height-ListField-solved/m-p/223906


Use Search. "Accept Solution" If the problem is resolved.