How to custom go newline when calling drawtext()? - blackberry

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

Related

Blackberry custombutton on touch/trackpad frozen screen

I created a sort of a grid with multiple vertical and horizontal fieldmanagers.
The code to create the grid is:
public class CategoriasScreen extends MainScreen {
private int colores [] = {Color.BLUE, Color.ALICEBLUE, Color.CHOCOLATE, Color.DARKORANGE, Color.YELLOW};
private int rows;
public CategoriasScreen(){
VerticalFieldManager row;
CategoriaFieldManager cat;
HorizontalFieldManager par;
EncodedImage eImage;
LabelField lf;
rows = 1;
Font font = getFont().derive(Font.PLAIN, Font.getDefault().getHeight() - 8);
setTitle("Categorias");
for(int i = 0; i < rows; i++){
row = new VerticalFieldManager(Field.FIELD_VCENTER | Manager.USE_ALL_WIDTH | Field.NON_FOCUSABLE);
par = new HorizontalFieldManager(Field.FIELD_HCENTER | Field.NON_FOCUSABLE);
cat = new CategoriaFieldManager(i);
eImage = EncodedImage.getEncodedImageResource("img/categoria" + i +".png");
eImage = LoaderScreen.resize(eImage, (int) Display.getWidth() / 5, (int) Display.getHeight() / 5, true);
cat.add(new BitmapField(eImage.getBitmap()));
lf = new LabelField("Categoria " + i, Field.FIELD_HCENTER);
lf.setFont(font);
cat.add(lf);
par.add(cat);
cat = new CategoriaFieldManager(i + 2);
eImage = EncodedImage.getEncodedImageResource("img/categoria" + (i + 2) +".png");
eImage = LoaderScreen.resize(eImage, (int) Display.getWidth() / 5, (int) Display.getHeight() / 5, true);
cat.add(new BitmapField(eImage.getBitmap()));
lf = new LabelField("Categoria " + (i + 2), Field.FIELD_HCENTER);
lf.setFont(font);
cat.add(lf);
par.add(cat);
row.add(par);
add(row);
//add(cat);
}
}
}
For the element inside the grid i extended a VerticalFieldManager and implemented the FieldChangeListener:
public class CategoriaFieldManager extends VerticalFieldManager implements FieldChangeListener{
private int colores [] = {Color.BLUE, Color.ALICEBLUE, Color.CHOCOLATE, Color.DARKORANGE, Color.YELLOW};
private int _idCategoria;
public CategoriaFieldManager(int idCategoria){
super(Field.FOCUSABLE);
_idCategoria = idCategoria;
setBackground(BackgroundFactory.createSolidBackground(colores[0]));
this.setPadding(new XYEdges(15,30,10,30));
this.setBorder(BorderFactory.createSimpleBorder(new XYEdges(20,20,20,20), Border.STYLE_TRANSPARENT));
//cat.setCookie(new Integer(i));
this.setChangeListener(this);
}
protected void sublayout(int width, int height){
super.sublayout((int) Display.getWidth() / 5 , (int) Display.getWidth() / 5);
setExtent((int) Display.getWidth() / 5, (int) Display.getWidth() / 5);
}
protected void onFocus(int direction){
setBackground(BackgroundFactory.createSolidBackground(colores[1]));
}
protected void onUnfocus(){
setBackground(BackgroundFactory.createSolidBackground(colores[0]));
}
public boolean isFocusable(){
return true;
}
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen( new ListadoEstablecimientosScreen(_idCategoria) );
/*UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run() {
Dialog.alert("hola mundo");
}
});*/
}
}
The problem that I've found is that when I touch the screen (anywhere) and then use the trackpad the phone freezes, I have to restart it to use it again. Then comes a notification that says the application was using too many resources. If it helps I've been testing in a BlackBerry Bold 9900

wrap text of custom listfield

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.

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

blackberry lazyloading in clumsy layout

I am using a customlistfield by extending VerticalFieldManager, and for the row I am usingf another CustoRowManager that extends Manager. The CustomRowManager has 2 TextField and a BitmapField . This list is implemented for a live xml feed. For loading the bitmapfield I am displaying placeholders which is then replaced by the actual image from background thread. I am not able to figure out how I should replace place holders with the bitmapfield. using invalidate() is one option, but on which element I should perform it.
public class CustomList extends VerticalFieldManager{
private int totalHeight = 0;
private int screenHeight = 0;
private int scrollPosition = 0;
protected void sublayout(int width, int height)
{
width = 480;
height = 230;
super.sublayout(width,height);
setExtent(width, height);
}
protected boolean navigationClick(int status, int time)
{
int index;
System.out.println("CLICKED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY)
{
index = this.getFieldWithFocusIndex();
UiApplication.getUiApplication().pushScreen(new TopNewsScreen(_vec, index));
}
return true;
}
Bitmap _bmp = null;
BitmapField _bmF = null;
Object _Obj = null;
TopNews _topNews = null;
String _headStr = null, _metaStr = null;
CustTextField _headNews = null, _metaData = null;
CustomRow _custRow = null;
Vector _vec;
public CustomList(Vector _vec,long _property)
{
super(_property);
this._vec = new Vector();
this._vec = _vec;
int _size = _vec.size();
int i ;
for(i = 0; i <_size; i++)
{
_headStr = new String();
_metaStr = new String();
_bmp = Bitmap.getBitmapResource("img/1.jpg");
_bmF = new BitmapField(_bmp);
_topNews = (TopNews)_vec.elementAt(i);
_headStr = _topNews.getHeadline();
_metaStr = _topNews.getMetaData();
int _newsLeng = _headStr.length(), alert = 0;
if(_newsLeng <= 35)
{
alert = 0;
}else if(_newsLeng>35&&_newsLeng<=70)
{
alert = 1;
}else {
alert = 2;
_headStr = this.truncate(_headStr,70);
}
_custRow = new CustomRow(alert);
_headNews = new CustTextField(_headStr,25,0x05235b,TextField.NON_FOCUSABLE);
_metaData = new CustTextField(_metaStr,15,0x666666,TextField.NON_FOCUSABLE);
_custRow.add(_headNews);
_custRow.add(_metaData);
_custRow.add(_bmF);
super.add(_custRow);
}
}
String truncate(String value, int length)
{
if (value != null && value.length() > length)
value = value.substring(0, length);
return value;
}
}
class CustomRow extends Manager implements FocusChangeListener{
private int _headLeng;
private NullField _focus = null;
CustomRow(int _headLeng)
{
super(Manager.FOCUSABLE|
Manager.NO_HORIZONTAL_SCROLL
|Manager.NO_VERTICAL_SCROLL);
this._headLeng = _headLeng;
_focus = new NullField(NullField.FOCUSABLE);
_focus.setFocusListener(CustomRow.this);
this.add(_focus);
}
protected void sublayout(int width, int height)
{
if(_headLeng==0)
{
layoutChild(getField(0),0,0);
setPositionChild(getField(0),0 , 0);
layoutChild(getField(1),360,20);
setPositionChild(getField(1),10 , 10);
layoutChild(getField(2),300, 20);
setPositionChild(getField(2), 10, 65);
layoutChild(getField(3),90,65);
setPositionChild(getField(3), 380, 10);
}else
{
/* layoutChild(getField(0),0,0);
setPositionChild(getField(0),0 , 0);
layoutChild(getField(1),360,20);
setPositionChild(getField(1),10 , 10);
layoutChild(getField(2),300, 20);
setPositionChild(getField(2), 10, 50);
layoutChild(getField(3),90,65);
setPositionChild(getField(3), 380, 10); */
layoutChild(getField(0),0,0);
setPositionChild(getField(0),0 , 0);
layoutChild(getField(1),360,20);
setPositionChild(getField(1),10 , 10);
layoutChild(getField(2),300, 20);
setPositionChild(getField(2), 10, 65);
layoutChild(getField(3),90,65);
setPositionChild(getField(3), 380, 10);
}
height = 80;
width = 480;
setExtent(width, height);
}
protected void paint(Graphics graphics)
{
graphics.setColor(Color.GRAY);
graphics.drawLine(10, 79, Display.getWidth()-10, 79);
super.paint(graphics);
}
public void focusChanged(Field field, int eventType) {
// TODO Auto-generated method stub
this.getManager().invalidate();
}
protected void paintBackground(Graphics g) {
int prevBg = g.getBackgroundColor();
if (_focus.isFocus()) {
g.setBackgroundColor(Color.LIGHTBLUE);
} else {
g.setBackgroundColor(Color.WHITE);
}
g.clear();
g.setBackgroundColor(prevBg);
}
}
the below link has answer for the question, I should change the name to Blackberry lazyloading.
http://supportforums.blackberry.com/t5/Java-Development/Clumsy-layout-invalidate/m-p/1398763

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

Resources