wrap text of custom listfield - blackberry

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::
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 ::

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.

Related

How to custom go newline when calling drawtext()?

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
}
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();

ListField item background Color

I just want to know how can I change ListField's item background color. I have two items in my ListField like this one.
|First One|Second One.................|
I need to change first one's background color.
My drawListRow(..) method looks like this
public void drawListRow(ListField listField, Graphics graphics,
int index, int y, int width) {
int oldColor = 0;
try {
oldColor = graphics.getColor();
String txt = (vector.elementAt(index)).toString();
int xPos = 15;
int yPos = 5 + y;
//graphics.clear();
graphics.setColor(Color.GREEN);
graphics.fillRect(0, y, (Display.getWidth()*10/100), yPos);
graphics.drawText(txt, xPos, yPos);
//graphics.fillRect(0,(index*Display.getHeight()/10),Display.getWidth(),Display.getHeight()/10);
} finally {
graphics.setColor(oldColor);
}
}
But this is not working.
Though you have attached an image, I am still confused. The image didn't answer some question, for example, how it will look on a row get focused (I didn't understand actually).
But you can check following output and code. I think you can customize the look as you wish if you check the code.
Generated Output
How to use
public class MyScreen extends MainScreen {
private Vector listElements;
public MyScreen() {
setTitle("Custom ListField Demo");
// data for the ListField
listElements = new Vector();
for (int i = 0; i < 4; i++) {
listElements.addElement("Some text for row " + i);
}
ListField taskList = new ListField() {
// disable default focus drawing
protected void drawFocus(Graphics graphics, boolean on) {
};
};
taskList.setCallback(new ListCallback(listElements));
taskList.setSize(listElements.size());
taskList.setRowHeight(40);
add(taskList);
}
}
ListCallback implementation
class ListCallback implements ListFieldCallback {
final int COLOR_INDEX_NORMAL_BG = 0x1D6789;
final int COLOR_INDEX_FOCUSED_BG = 0x0E8CB3;
final int COLOR_NORMAL_BG = 0x2A2A2A;
final int COLOR_FOCUSED_BG = 0x1F1F1F;
private Vector listElements;
public ListCallback(Vector listElements) {
this.listElements = listElements;
}
public void drawListRow(ListField list, Graphics graphics, int index, int y,
int width) {
int rowHeight = list.getRowHeight(index);
boolean isSelectedRow = (list.getSelectedIndex() == index);
int indexBgColor = isSelectedRow ? COLOR_INDEX_FOCUSED_BG : COLOR_INDEX_NORMAL_BG;
int rowBgColor = isSelectedRow ? COLOR_FOCUSED_BG : COLOR_NORMAL_BG;
final int indexWidth = width / 10;
// draw row background
fillRectangle(graphics, rowBgColor, 0, y, width, rowHeight);
// draw index background
fillRectangle(graphics, indexBgColor, 0, y, indexWidth, rowHeight);
// set text color, draw text
Font font = list.getFont();
graphics.setColor(Color.WHITE );
graphics.setFont(font);
String indexText = "" + (index + 1);
String textToDraw = "";
try {
textToDraw = (String) listElements.elementAt(index);
} catch (Exception exc) {
}
int xText = (indexWidth - font.getAdvance(indexText)) / 2;
int yText = (rowHeight - font.getHeight()) / 2;
graphics.drawText(indexText, xText, y + yText, 0, indexWidth);
final int margin = 5;
int availableWidth = (width - indexWidth) - 2 * margin;
xText = indexWidth + margin;
yText = (rowHeight - font.getHeight()) / 2;
graphics.drawText(textToDraw, xText, y + yText, DrawStyle.ELLIPSIS, availableWidth);
}
private void fillRectangle(Graphics graphics, int color, int x, int y, int width, int height) {
graphics.setColor(color);
graphics.fillRect(x, y, width, height);
}
public Object get(ListField list, int index) {
// not implemented
return "";
}
public int indexOfList(ListField list, String prefix, int string) {
// not implemented
return 0;
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
}
If you need to change onFocus Background color than add drwFocus method on your ListField.
protected void drawFocus(Graphics graphics, boolean on) {
//get the focus rect area
XYRect focusRect = new XYRect();
getFocusRect(focusRect);
boolean oldDrawStyleFocus = graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
try {
if (on) {
//set the style so the fields in the row will update its color accordingly
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
int oldColour = graphics.getColor();
try {
graphics.setColor(0xc8d3db); //set the color and draw the color
graphics.fillRect(focusRect.x, focusRect.y,
focusRect.width, focusRect.height);
} finally {
graphics.setColor(oldColour);
}
//to draw the row again
drawListRow(this, graphics, getSelectedIndex(),
focusRect.y, focusRect.width);
// drawRow(graphics, focusRect.x,focusRect.y, focusRect.width,focusRect.height);
}
} finally {
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
}
}
Check the edited answer,
protected void drawFocus(Graphics graphics, boolean on) {
XYRect focusRect = new XYRect();
getFocusRect(focusRect);
boolean oldDrawStyleFocus = graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
try {
if (on) {
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
int oldColour = Color.BLACK;
try {
graphics.fillRect(focusRect.x, focusRect.y,
focusRect.width, focusRect.height);
} finally {
graphics.setColor(oldColour);
}
//to draw the row again
drawListRow(this, graphics, getSelectedIndex(),
focusRect.y, focusRect.width);
}
} finally {
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
}
}

How to develop custom List Field in Blackberry

In my app i have to show list of items in list field when i click on specific item in the list field the background color for a particular row change to grey color.How to develop this type of custom list field in Blackberrry.Any one please give ideas.
Thank You
you should draw rectangle in listfield row which is selected.. some thing like this. Here i have done it for on focus..
public void drawListRow(ListField list, Graphics g, int index, int y,int w) {
if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
{
g.setBackgroundColor(0x00572000);
//g.setBackgroundColor();
g.clear();
//g.setColor(Color.BLACK);
// g.fillRect(0,list.getWidth(),list.getWidth(),80);
g.setColor(Color.ORANGE);
g.fillRect(94,y+0,400,30);
//g.setColor(0x000000);
g.setColor(Color.WHITE);
g.drawText(text, 95, y+10, (DrawStyle.LEFT ), w );
}
else
{
g.setColor(0x00906966);
g.fillRect(94,y+0,400,30);
g.setColor(Color.ORANGE);
g.drawText(text, 95, y+10, (DrawStyle.LEFT ), w );
}}
Try this ...
private class MyListField extends ListField{
//0,ListField.MULTI_SELECT
private boolean hasFocus = false;
public void onFocus(int direction){
hasFocus = true;
}
public void onUnfocus()
{
hasFocus = false;
super.onUnfocus();
invalidate();
}
public void paint(Graphics graphics)
{ int width = Display.getWidth();
//Get the current clipping region
XYRect redrawRect = graphics.getClippingRect();
if(redrawRect.y < 0)
{
throw new IllegalStateException("Error with clipping rect.");
}
//Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();
int curSelected;
//If the ListeField has focus determine the selected row.
if (hasFocus)
{
curSelected = getSelectedIndex();
}
else
{
curSelected = -1;
}
int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getSize() - 1);
int y = startLine * rowHeight;
//Setup the data used for drawing.
int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight};
int[] xInds = new int[]{0, width, width, 0};
//Set the callback - assuming String values.
ListFieldCallback callBack = this.getCallback();
//Draw each row
for(; startLine <= endLine; ++startLine)
{
//If the line we're drawing is the currentlySelected line then draw the fill path in LIGHTYELLOW and the
//font text in Black.
if(startLine == curSelected){
graphics.setColor(Color.LIGHTYELLOW);
graphics.drawFilledPath(xInds, yInds, null, null);
graphics.setColor(Color.BLACK);
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
}
else{
//Draw the odd or selected rows.
graphics.setColor(Color.LIGHTGREY);
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
}
//Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
//super.paint(graphics);
}
}
.
.
.
refer this [LINK] : http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html

Blackberry: why is drawListRow() called with different y for ListField and KeywordFilterField

I'm trying to move an app from using a KeywordFilterField to ListField and I'm struggling since several hours to find out, why is drawListRow() called with different y values - depending on which of these two ListField's I use:
If getRowHeight() returns 40, then the y values will be -
For KeywordFilterField are: 0; 40; 80; 120; ... (i.e. as expected)
But for Listfield I see: 9; 49; 89; 129; ... (i.e. offset by 9 for some reason)
Where is the 9 coming from? Is there a method in ListField or ListFieldCallback which I could call to get this value? I'm just trying to draw a light gray line between items of the list.
Below is my test code and the border.png (used as BasicEditField border) is attached:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final int EXTRA_ROWS = 2;
MyItemList myItems = new MyItemList();
ListField myList = new ListField(EXTRA_ROWS);
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(12, 12, 12, 12),
Bitmap.getBitmapResource("border.png"));
Background myBg = BackgroundFactory.createSolidBackground(0x111111);
StringProvider myProvider = new StringProvider("Search");
BasicEditField myFind = new BasicEditField(USE_ALL_WIDTH) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText(myProvider.toString(), 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
public MyScreen() {
getMainManager().setBackground(myBg);
myFind.setBorder(myBorder);
setTitle(myFind);
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setCallback(new MyListFieldCallback());
add(myList);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
System.err.println("XXX index=" + index+ ", y=" + y + ", width=" + width);
g.setColor(Color.WHITE);
if (index < EXTRA_ROWS) {
Font i = getFont().derive(Font.ITALIC);
g.setFont(i);
g.drawText("Add Item", 0, y);
return;
}
if (index >= EXTRA_ROWS) {
MyItem item = (MyItem) myItems.getAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y);
g.setColor(0x333333);
// XXX why do I need to subtract 9 here?
g.drawLine(0, y-9, width, y-9);
return;
}
g.drawText(list.getEmptyString(), 0, y);
}
public Object get(ListField list, int index) {
return myItems.getAt(index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField list, String prefix, int start) {
return 0;
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
myList.setSize(size() + EXTRA_ROWS);
}
protected boolean doRemove(Object obj) {
myList.setSize(size() - 1 + EXTRA_ROWS);
return super.doRemove(obj);
}
}
}
class MyItem {
int _num;
String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
return item1.toString().compareTo(item2.toString());
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
}
}
The produced output is:
[ 64,890] XXX index=0, y=9, width=360
[ 64,890] XXX index=1, y=49, width=360
[ 64,898] XXX index=2, y=89, width=360
[ 64,898] XXX index=3, y=129, width=360
[ 64,906] XXX index=4, y=169, width=360
[ 64,906] XXX index=5, y=209, width=360
UPDATE in reply to jprofitt
When I try your suggestion (I use red color for your text and lines):
if (index >= EXTRA_ROWS) {
MyItem item = (MyItem) myItems.getAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y);
g.setColor(Color.RED);
g.drawText("XXX", 0, y + (list.getRowHeight() - list.getFont().getHeight())/2);
g.setColor(0x333333);
// XXX why do I need to subtract 9 here?
g.drawLine(0, y-9, width, y-9);
g.setColor(Color.RED);
g.drawLine(0, y, width, y);
return;
}
Then it doesn't really work - because the blue focus line does not align with your suggested (red) lines. It aligns with my (gray) lines, which means you really need to subtract -9 for some reason:
Thank you!
Alex
Yes, this is an odd behaviour. I guess this is smth OS 6 specific. Looks like in OS 6 ListField became so clever that it passes Y coordinate already prepared for direct usage in text drawing, so you don't have to do manual calculation (usually I calculate Y for text drawing in the same way jprofitt suggests). So assuming my guess is true I changed the code as follows:
if (index >= EXTRA_ROWS) {
MyItem item = (MyItem) myItems.getAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y);
g.setColor(0x333333);
// XXX why do I need to subtract 9 here?
// use the offset instead
int offset = (myList.getRowHeight() - getFont().getHeight()) >> 1;
g.drawLine(0, y - offset, width, y - offset);
return;
}
and it works fine (tested on all font sizes that are available in device settings).
Alright I believe I got this figured out. What is going on is that your row height is greater than the font height. So when you draw your text right at y, you are drawing it top aligned to the actual row. In my case, row height was 40 and font height was 20. Half of that difference is where your y - 9 was coming in. If you change your drawText() calls to this, it should work without needing to subtract anything when drawing the line:
g.drawText(theString, 0, y + (list.getRowHeight() - list.getFont().getHeight())/2);
You could cache the font height and row height so you don't have to do the calculations in paint(), for efficiency's sake.

it displays only text,but not the image

i developed the code as below.in this i used listfield ,one bitmapfield and one label field,when i will run it ,it displays only text on the list field row,but not the image
i don't know where i did mistake,so,plz,any one help me to know where i did mistake
thanks for any help
class TaskListField extends MainScreen implements ListFieldCallback {
private Vector rows;
private Bitmap p1;
ListField list;
TableRowManager row;
public TaskListField() {
super();
list=new ListField() {
protected void drawFocus(Graphics graphics, boolean on) {
}
};
list.setRowHeight(40);
list.setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
list.setCallback(this);
p1 = Bitmap.getBitmapResource("res/images/10.png");
rows = new Vector();
for (int x = 1; x < 13; x++) {
row = new TableRowManager();
LabelField task = new LabelField("" + String.valueOf(x),
DrawStyle.ELLIPSIS);
row.add(task);
row.add(new BitmapField(p1));
rows.addElement(row);
}
list.setSize(rows.size());
add(list);
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y,
int width) {
TableRowManager rowManager = (TableRowManager) rows
.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(g);
g.setColor(0x00CACACA);
g.drawLine(0, 0, getPreferredWidth(), 0);
// Restore the graphics context.
g.popContext();
}
protected void sublayout(int width, int height) {
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, 30, 30);
setPositionChild(field, 0, 0);
field = getField(1);
layoutChild(field, 40, 25);
setPositionChild(field, 120, 10);
setExtent(preferredWidth, getPreferredHeight());
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth() {
return Graphics.getScreenWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight() {
return list.getRowHeight();
}
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
Are you sure the bitmap is not null? I would check that first -- perhaps it's not finding the resource.
Could the LabelField be taking the entire width of the screen (it does that on certain situations)? When you only set the image without setting the label, does the image show?
The image path is not necessary as you put it
p1 = Bitmap.getBitmapResource (" res/images/10.png ");
only needs
p1 = Bitmap.getBitmapResource (" 10.png ");

Resources