11-03-2009 10:53 AM
I've got an objectlistfield which shows a bunch of selectable items which can be selected. The problem with it is that each items text is very long and needs at least two lines and the objectlistfield seems not to support this feature. It just cuts the text.
I tried increasing each rows height but the text does not change it's format. I've read that a possible solutions could be to overwrite the "drawListRow" from the objectlistfield but i cannot figure how to do that correctly.
I'll appreciate any kind of help.
Solved! Go to Solution.
11-03-2009 11:03 AM
first, you have to set the row height. I usually take double the font size and add 2 for good measure.
now you can adjust the drawListRow. You get an y parameter that determines where in your current row you are. drawText takes both x and y as input. you can draw the first line using drawText(5,y,text) and the second using drawText(5, y+listField.getRowHeight/2, text2). You have to take care of a correct linebreak etc, i usually do this with a StringTokenizer.
11-03-2009 11:26 AM
I this is waht you mean, right? My doubt now is how can I access to FIRST_LINE_TEXT and SECOND_LINE_TEXT?
listaPtos = new ObjectListField()
{
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
graphics.drawText(FIRST_LINE_TEXT, 5, y);
graphics.drawText(SECOND_LINE_TEXT, 5, y + listField.getRowHeight() /2 );
}
};Thanks for your fast reply!
11-03-2009 11:33 AM
i don't work with the standard listfield anymore, using an own implementation that makes things easier.
anyhow, you have the listfield and the index, thus you can retrieve the object that should be drawn. you can read its attributes or, if it is a string, handle it directly.
11-03-2009 11:37 AM
That should work, though I would probably calculate the increase in Y for the second call to drawText() by adding graphics.getFont().getHeight() instead of what you're doing.
Another observation I'd have is that you should avoid hardcoded pixel counts like the 5 you have for X or the 2 that the other reply suggested as padding. The reason is that font sizes vary quite dramatically between different devices so your spacing should always be calculated in proportion to the font size if you want the layout to look consistent between different devices.
I'm not really sure what you mean about getting access to the first and second line. Do you mean, how do you actually get the text for the current list item? You just use the index that's passed to drawListItem() and invoke get() on the ObjectListField.
11-04-2009 02:37 AM
Thanks everybody I could solve my issue. I had problems using font height so I finally used the Y parameter.