0

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
}
Alan Lai
  • 1,296
  • 2
  • 12
  • 16
  • 1
    Check this answer - http://stackoverflow.com/q/7912880/431639. Also check this, http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/td-p/258808. – Rupak Jul 18 '12 at 16:49

1 Answers1

0

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();
Nate
  • 31,017
  • 13
  • 83
  • 207
  • it does not work, the text still overlap the image. Is this support utf-8 text? my text is chinese word. – Alan Lai Jul 19 '12 at 08:28
  • @AlanLai, I thought that your problem was that you needed to display text on more than one line? The code above just splits text with '\n' characters inside onto more than one line. It doesn't do anything with drawing bitmaps. If the text is overlapping your bitmap improperly, the x and y values that are passed to `drawText()` must be wrong. In my example those values are `_padding` and `top`. Take a look at those and move them until they are on top of your image. I'm not sure about `drawText()` and UTF-8 characters. But, that's the same method you were using in the question. – Nate Jul 19 '12 at 08:41
  • I see. What I want is a function that will draw the texts into multiple lines and return the height of the texts – Alan Lai Jul 19 '12 at 09:16
  • @AlanLai, do you want the height as (1) the total number of pixels, or (2) as the number of lines? If you want the total number of pixels, do you just want the number of lines times the height of the font, or do you want the result to include the vertical padding/spacing between lines? – Nate Jul 19 '12 at 09:19
  • [this](http://img444.imageshack.us/img444/5623/sc20120719172309.png) is what I want. Look at the Black text beside the image. It was `title[index]` – Alan Lai Jul 19 '12 at 09:27
  • @AlanLai, ok, so I see text that takes up 3 lines. That's fine. Why doesn't the code I posted do that? Did you put the '\n' characters into the `title[index]` string, as I said in my answer above? The code searches for '\n', so the string must have newlines put in for the code to draw on more than one line. If you want the code to figure out where to put the newline breaks, let me know ... that's not what the code above does. – Nate Jul 19 '12 at 09:52
  • This is the problem. I not sure which text will be the last character of the row. The idea is the text being draw until detect the available space cannot fit next character then auto insert `\n` and continue draw. I not sure you understand or not. This can be done in android because default will do. – Alan Lai Jul 19 '12 at 10:11
  • @AlanLai, I don't know Chinese. What are the rules for breaking a line? Is it ok to break between any two characters? Do you need to break only on spaces, like in English? – Nate Jul 19 '12 at 10:30
  • assume this `String` `"1234567890 abcdefghijklmn"` as the `title[index]`. Just treat the `whitespace` as a character. When `width` unable to draw next character then insert `\n` and start in next line. – Alan Lai Jul 19 '12 at 10:47
  • I just use [this](http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/td-p/258808) example. It work with default font size but not custom fontsize. – Alan Lai Jul 19 '12 at 11:17
  • @AlanLai, so do you still need help on this one? Or did the example you linked to solve your problem? – Nate Jul 20 '12 at 02:50
  • actually is solved half, the example is calculating with default font size. The calculation went wrong when I set custom font size. I hope you can help me to custom the function with accept custom font size. – Alan Lai Jul 20 '12 at 06:12
  • @AlanLai, sure ... let me modify the code, now that I understand what you need. – Nate Jul 20 '12 at 06:33
  • I am using the answer with `getsplitindex()`. – Alan Lai Jul 20 '12 at 06:50