0

i need to make list of news so i have implement custom listfield with one thumb and two text and it draw with graphics object.it all gone fine and give me result as expect but there are something problem with text wraping i am basically android developer and there are wrap content file allow that set textfiled automatically but in this case there are bind one text over second. i have refer customer listfield from here

Here is screen::

enter image description here

Code::

public class CustomListField extends ListField implements ListFieldCallback {

    private Vector _listData;

    private int _MAX_ROW_HEIGHT = 80;

    public CustomListField(Vector data) {

        _listData = data;

        setSize(_listData.size());

        setSearchable(true);

        setCallback(this);

        setRowHeight(_MAX_ROW_HEIGHT);

    }

    protected void drawFocus(Graphics graphics, boolean on) {

        XYRect rect = new XYRect();

        graphics.setGlobalAlpha(150);

        graphics.setColor(Color.BLUE);

        getFocusRect(rect);

        drawHighlightRegion(graphics, HIGHLIGHT_FOCUS, true, rect.x, rect.y,
                rect.width, rect.height);

    }

    public int moveFocus(int amount, int status, int time) {

        this.invalidate(this.getSelectedIndex());

        return super.moveFocus(amount, status, time);

    }

    public void onFocus(int direction) {

        super.onFocus(direction);

    }

    protected void onUnFocus() {

        this.invalidate(this.getSelectedIndex());

    }

    public void refresh() {

        this.getManager().invalidate();

    }

    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int w) {

        ListRander listRander = (ListRander) _listData.elementAt(index);

        graphics.setGlobalAlpha(255);

        graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN,
                24));

        final int margin = 5;

        final Bitmap thumb = listRander.getListThumb();

        final String listHeading = listRander.getListTitle();

        final String listDesc = listRander.getListDesc();

        final String listDesc2 = listRander.getListDesc2();

        final Bitmap nevBar = listRander.getNavBar();

        // list border

        graphics.setColor(Color.GRAY);

        graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT);

        // thumbnail border & thumbnail image

        graphics.setColor(Color.BLACK);

        graphics.drawRoundRect(margin - 2, y + margin - 2,
                thumb.getWidth() + 2, thumb.getHeight() + 2, 5, 5);

        graphics.drawBitmap(margin, y + margin, thumb.getWidth(), thumb
                .getHeight(), thumb, 0, 0);

        // drawing texts

        //graphics.setFont(FontGroup.fontBold);

        graphics.drawText(listHeading, 2 * margin + thumb.getWidth(), y
                + margin);

        graphics.setColor(Color.GRAY);

        //graphics.setFont(FontGroup.smallFont);

        graphics.drawText(listDesc, 2 * margin + thumb.getWidth(), y + margin
                + 20);

        graphics.drawText(listDesc2, 2 * margin + thumb.getWidth(), y + margin
                + 32);

        // draw navigation button

        final int navBarPosY = y
                + (_MAX_ROW_HEIGHT / 2 - nevBar.getHeight() / 2);

        final int navBarPosX = Graphics.getScreenWidth() - nevBar.getWidth()
                + margin;

        graphics.drawBitmap(navBarPosX, navBarPosY, nevBar.getWidth(), nevBar
                .getHeight(), nevBar, 0, 0);

    }

    public Object get(ListField listField, int index) {

        String rowString = (String) _listData.elementAt(index);

        return rowString;

    }

    public int indexOfList(ListField listField, String prefix, int start) {

        for (Enumeration e = _listData.elements(); e.hasMoreElements();) {

            String rowString = (String) e.nextElement();

            if (rowString.startsWith(prefix)) {

                return _listData.indexOf(rowString);

            }

        }

        return 0;

    }

    public int getPreferredWidth(ListField listField) {

        return 3 * listField.getRowHeight();
    }
}

/*
 * protected boolean trackwheelClick (int status, int time) {
 * 
 * invalidate(getSelectedIndex());
 * 
 * Dialog.alert(" U have selected :" + getSelectedIndex());
 * 
 * return super.trackwheelClick(status, time);
 * 
 * }

Update:

new screen :: enter image description here

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • Welcome to Stack Overflow. Please read [How to Ask](http://stackoverflow.com/questions/how-to-ask), [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/), [How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html). –  Jul 19 '12 at 09:50
  • I am going to post code here, please remove comment – Nikunj Patel Jul 19 '12 at 09:52
  • decrease the font size using FontFamily fontFamily[] = FontFamily.getFontFamilies(); Font font = fontFamily[1].getFont(FontFamily.CBTF_FONT, 15); – Raj Kamal Jul 19 '12 at 10:37
  • Use this link, http://stackoverflow.com/a/1881319/431639. Study the code carefully, it is a nice one. Check how each row draws itself using delegate manager. – Rupak Jul 19 '12 at 11:48
  • @Rupak yes there are 80% problem are solved but now it display text in Lablfield like "List Name ....." so what i do for display full text. – Nikunj Patel Jul 20 '12 at 06:02
  • If you are using the solution provided in the above link, then you only need to change the construction of all the `LabelField`s only. Just don't use `DrawStyle.ELLIPSIS` style to construct a `LabelField` if you want to display full text. – Rupak Jul 20 '12 at 06:10
  • @Rupak now it display entire text but its out of list item boundary you can see my update question ... – Nikunj Patel Jul 20 '12 at 06:23
  • @Rupak so the question is how can i increase automatically its listrow height as per text size – Nikunj Patel Jul 20 '12 at 06:25
  • It is not possible to set different height for each rows in `ListField`. So you have to determine the height of the FieldManager you are using as a row. But if you really need different height for different rows, then you need to use `VerticalFieldManager` (extremely slower approach). – Rupak Jul 20 '12 at 06:34

1 Answers1

1

You need to do it programmatic , We don't have label field in a list because we are rendering it using graphics. So i used to do a workaround to do so , i calculate the no of pixels availabale for my custom font for text , then how much space it will take i will give with three period signs. You can use this code if , it can help you, use it under drawListRow method

String name =(String)ht.get("title");
xTotal= f2.getAdvance(name);
xAvail= Display.getWidth() - <bitmap>.getWidth() - 30;
if(xTotal > xAvail)
{
forLabel= name.length() * xAvail / xTotal ;
    name = name.substring(0, forLabel - 3) + "...";

}

use name string variable in place of graphics.drawText.

Shashank Agarwal
  • 512
  • 3
  • 12