Question

This is a listfield.

public class Custom_ListField extends ListField {
private String[] title, category, date, imagepath;
private int[] newsid, catsid;
private List_News newslist;
private Bitmap imagebitmap[], localimage = Bitmap
        .getBitmapResource("image_base.png");
private BrowserField webpage;
private Custom_BrowserFieldListener listener;
private boolean islatest;

private Vector content = null;
private ListCallback callback = null;

private int currentPosition = 0;

public Custom_ListField(Vector content, boolean islatest) {
    this.content = content;
    this.islatest = islatest;
    newsid = new int[content.size()];
    title = new String[content.size()];
    category = new String[content.size()];
    date = new String[content.size()];
    imagepath = new String[content.size()];
    catsid = new int[content.size()];
    imagebitmap = new Bitmap[content.size()];

    for (int i = 0; i < content.size(); i++) {
        newslist = (List_News) content.elementAt(i);
        newsid[i] = newslist.getID();
        title[i] = newslist.getNtitle();
        category[i] = newslist.getNewCatName();
        date[i] = newslist.getNArticalD();
        imagepath[i] = newslist.getImagePath();

        if (!imagepath[i].toString().equals("no picture")) {
            imagebitmap[i] = Util_ImageLoader.loadImage(imagepath[i]);
        } else {
            imagebitmap[i] = localimage;
        }
        catsid[i] = newslist.getCatID();
    }

    initCallbackListening();
    this.setRowHeight(localimage.getHeight() + 10);
}

private void initCallbackListening() {
    callback = new ListCallback();
    this.setCallback(callback);
}

private class ListCallback implements ListFieldCallback {

    public ListCallback() {
        setBackground(Config_GlobalFunction
                .loadbackground("background.png"));
    }

    public void drawListRow(ListField listField, Graphics graphics,
            int index, int y, int width) {
        currentPosition = index;
        graphics.drawBitmap(
                Display.getWidth() - imagebitmap[index].getWidth() - 5,
                y + 3, imagebitmap[index].getWidth(),
                imagebitmap[index].getHeight(), imagebitmap[index], 0, 0);
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, y, width, imagebitmap[index].getHeight() + 10);



        graphics.setColor(Color.BLACK);
        graphics.setFont(Font.getDefault().derive(Font.BOLD, 20));
        graphics.drawText(title[index], 5, y + 3, 0, Display.getWidth()
                - imagebitmap[index].getWidth() - 10);

        graphics.setColor(Color.GRAY);
        graphics.setFont(Font.getDefault().derive(Font.BOLD, 15));
        graphics.drawText(date[index], 5, y + 6
                + Font.getDefault().getHeight() + 3);

        if (islatest) {
            graphics.setColor(Color.RED);
            graphics.setFont(Font.getDefault().derive(Font.BOLD, 15));
            graphics.drawText(category[index], Font.getDefault()
                    .getAdvance(date[index]) + 3, y + 6
                    + Font.getDefault().getHeight() + 3);
        }
    }

    public Object get(ListField listField, int index) {
        return content.elementAt(index);
    }

    public int getPreferredWidth(ListField listField) {
        return Display.getWidth();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        return content.indexOf(prefix, start);
    }
}

public int getCurrentPosition() {
    return currentPosition;
}

protected boolean navigationClick(int status, int time) {
    int index = getCurrentPosition();
    if (catsid[index] == 9) {
        if (Config_GlobalFunction.isConnected()) {
            webpage = new BrowserField();
            listener = new Custom_BrowserFieldListener();
            webpage.addListener(listener);

            MainScreen aboutus = new Menu_Aboutus();
            aboutus.add(webpage);
            Main.getUiApplication().pushScreen(aboutus);

            webpage.requestContent("http://www.orientaldaily.com.my/index.php?option=com_k2&view=item&id="
                    + newsid[index] + ":&Itemid=223");
        } else
            Config_GlobalFunction.Message(Config_GlobalFunction.nowifi, 1);
    } else
        Main.getUiApplication().pushScreen(
                new Main_NewsDetail(newsid[index]));
    return true;
}
}

Please look at the

graphics.setColor(Color.BLACK);
graphics.setFont(Font.getDefault().derive(Font.BOLD, 20));
graphics.drawText(title[index], 5, y + 3, 0, Display.getWidth()
                - imagebitmap[index].getWidth() - 10);

This will only draw the text one line only. I did researched and found out there isn't built in function and must custom a function make the text auto next line.

The function something like this

private int numberoflines(int availablespace){
    ...
    return numberlines
}
Was it helpful?

Solution

The links Rupak shows are good, although one of them references the generic Java problem (and proposes a Swing result that would need to be changed for BlackBerry), and the other references an external (non stack overflow) link.

If you want another option, and don't want the algorithm to figure out where to make the line breaks, you can use this. This code assumes you put '\n' characters into your strings, where you want to split the text into multiple lines. You would probably put this code in the paint() method:

     // store original color, to reset it at the end
     int oldColor = graphics.getColor();

     graphics.setColor(Color.BLACK);
     graphics.setFont(_fieldFont);
     int endOfLine = _text.indexOf('\n'); 
     if (endOfLine < 0) {
        graphics.drawText(_text, _padding, _top);
     } else {
        // this is a multi-line label
        int top = _top;
        int index = 0;
        int textLength = _text.length();
        while (index < textLength) {
           // draw one line at a time
           graphics.drawText(_text,
                 index,             // offset into _text
                 endOfLine - index, // number of chars to draw
                 _padding,          // x
                 top,               // y
                 (int) (DrawStyle.HCENTER | DrawStyle.TOP | Field.USE_ALL_WIDTH), // style flags
                 _fieldWidth - 2 * _padding);                                     // width available

           index = endOfLine + 1;
           endOfLine = _text.indexOf('\n', index); 
           if (endOfLine < 0) {
               endOfLine = textLength;
           }
           top += _fieldFont.getHeight() + _top;  // top padding is set equal to spacing between lines
        }
     }

     graphics.setColor(oldColor);

And here you would initialize some of the variables I use in that. I think these are right, based on the code you posted, but you'll need to double-check:

     String _text = title[index];   // text to draw
     int _padding = 5;              // left and right side padding around text
     int _top = y + 3;              // the y coordinate of the top of the text

     Font _fieldFont = Font.getDefault().derive(Font.BOLD, 20);

     // the total width reserved for the text, which includes room for _padding:
     int _fieldWidth = Display.getWidth() - imagebitmap[index].getWidth();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top