views:

943

answers:

3

Hi all,

I m creating a ListField with Checkboxes 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 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 ?

+1  A: 

I think "calculate the number of lines depending on the row width" means:

  • split text on words
  • create string array and while words array not empty do
    • create string lineStr
    • 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)
Max Gontar
Ok. Thanks. I will try with this.
Shreyas
A: 

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 ?

Shreyas
I m calculating the string to draw in drawListRow(....). After it I m setting the Row Height using setRowHeight(int). But when i write this method in drawListRow(....) then the output comes properly but it goes in loop. So where should i call setRowHeight(int) ?
Shreyas
A: 

Hi Coldice,

With ur logic I got

* 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

I m doing it like

if(getFont().getAdvance(lineStr+" "+words[i]) < Display.getWidth())
{
    lineStr += words[i];
}

& I m storing lineStr in a Vector linesToDraw as the same lineStr I m using for storing text of next row.

& in the end for drawing I m doing

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

but how to calculate no. of lines of a row & row height?

so that with this row height I can write setRowHeight(rowHeight) as the row height is different for different rows.

Shreyas
Hi Shreyas. Row height is common for every row in ListField, we can't set height for separate row. The best choice is to set max height.
Max Gontar
Use "comment" on answer if you want to ping someone )) we don't receive notifications on new answers in others questions here.
Max Gontar