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: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

List text is wrapping when it is larger than the screen width

Hi,

 

I have a listfield. And each row, i am adding one text which i am getting from the library. So the length of the text is variying each time. Now if it is more lengthy, the text is going out of the screen rather coming to the next line. How can i make it to come in the next line?

 

Here is my code:

 

public class ListCallBack implements ListFieldCallback{

		
		public void drawListRow(ListField listField, Graphics graphics, int index,
		        int y, int width) {
		    // TODO Auto-generated method stub
		    String text = mAccounts[index].accountId + "'s ArcotID";
		    graphics.drawText(text, 20, y, DrawStyle.ELLIPSIS, width);
		    graphics.setFont(FONT_FAMILY_0_SF_AS_12);

		    if (index != 0) {
		    	graphics.setColor(Color.BLACK);
		    	graphics.drawLine(0, y-12, width, y-12);
			}
		}

		public Object get(ListField listField, int index) {
		    // TODO Auto-generated method stub
		    return mAccounts[index];
		}

		public int getPreferredWidth(ListField listField) {
		    // TODO Auto-generated method stub
		    return screenWidth;
		}
		
		public int getPreferredHeight() {
			return getContentHeight();
		}

		public int indexOfList(ListField listField, String prefix, int start) {
		    // TODO Auto-generated method stub
		    return listField.getSelectedIndex();
		}
}

 Please help.

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

Re: List text is wrapping when it is larger than the screen width

The 'trick' is this line here:

graphics.drawText(text, 20, y, DrawStyle.ELLIPSIS, width);

 

This will always only draw a single line.

 

If you want to output multiple lines then you have to detect which parts will not fit, and paint these on the next line, using something like:

 

graphics.drawText(textThatDidNotFit, 20, y+graphics.getFont.getHeight(), DrawStyle.ELLIPSIS, width);

 

Of course your row must be big enough to support the output on the second line - ListField rows do not grow or shrink - they are set to the size you have specified (or defaulted) when you create them.  See

<listField>.setRowHeight();

 

Questions similar to this have been asked before, please search the forum for details of previous solutions and samples of code that will 'wrap' text over multiple lines. 

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: List text is wrapping when it is larger than the screen width

the easiest solution is to use the ellipsis drawstyle, but you already have that in your code. however, you have x set to 20 and should subtract that from the width parameter.
graphics.drawText(text, 20, y, DrawStyle.ELLIPSIS, width-20);

writing your own code to create line breaks is quite some task, i've seen some implementations posted on the forums so maybe you can search for them.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

Re: List text is wrapping when it is larger than the screen width

But DrawStyle.ELIPSIS is only used to wrap the text and add some dotted points end of the text. Instead of this drawText can we add one labelField in the drawListRow()?

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

Re: List text is wrapping when it is larger than the screen width

DrawStyle.ELIPSIS doesn't wrap text, it just terminates it 'nicely with ... if it does not fit in the space provided. 

 

I suspect you would probably find it easier to wrap the text yourself than try to work the system to get a LabelField to do it for you. 

 

Either way, you will have the problem I first pointed out with respect to the height of a row. 

 

If you only have a few items in your List, perhaps ListField is not a good choice for you?