03-10-2009 07:41 PM
I have spent the better half of today trying to figure out how to set the background color for when a ListField item has focus. I have tried overriding paint and drowFocus and can not get any color other than the default.
Anyone know how to do this?
Solved! Go to Solution.
03-11-2009 06:07 AM
public void drawListRow(ListField list, Graphics g, int index, int y, int w) { if(list.getSelectedIndex() == index) { g.setColor(0x0000FF); g.fillRect(0,(index*rowHight),rowWidth,rowHeight); g.setColor(0x000000); } g.drawText("Text", 0, y, 0, w); }
PS: Call invalidate, if the previous paints remains on screen.
03-11-2009 01:27 PM
05-20-2009 03:33 PM
Hey BBDeveloper, i have the same problem and i tried what you said. It did work, but calling invalidate() seems to take up a lot of memory when i use the simulator. When i take out the call to invalidate() it takes up no memory BUT it doesn't repaint. Any suggestions?
This is my code:
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) { int offsetY = (this.getRowHeight() - icon.getHeight())/2; if(mainScreenMenu.getSelectedIndex() == index) { graphics.clear(); graphics.setColor(16777215); graphics.fillRect(0,(index*this.getRowHeight()), this.getWidth(), this.getRowHeight()); graphics.drawBitmap(0, y+offsetY, mainSelectionBmp[index*2].getWidth(), mainSelectionBmp[index*2].getHeight(), mainSelectionBmp[index*2], 0, 0); } else { graphics.clear(); graphics.drawBitmap(0, y+offsetY, icon.getWidth(), icon.getHeight(), icon, 0, 0); } this.invalidate(); }
05-20-2009 11:53 PM
Hi,
Regarding your invalidate issue use this overloaded form of invalidate() which takes index as its parameter
<listField instance>.invalidate(<index>).
Above mention solution provided works well just try to debug you code by removing graphics.clear.Remove all you lines and just right those lines which are mentioned in above solution and then one by one add line to make it work according to your requirement.
Let me know if you were able to solve it or not.
05-21-2009 03:03 PM
Hi Rajat,
I tried what you said, but the same thing is happening. Will this harm the device?
if(mainScreenMenu.getSelectedIndex() == index) { graphics.setColor(16777215); graphics.fillRect(0,(index*this.getRowHeight()), this.getWidth(), this.getRowHeight()); graphics.drawBitmap(0, y+offsetY, mainSelectionBmp[index*2].getWidth(), mainSelectionBmp[index*2].getHeight(), mainSelectionBmp[index*2], 0, 0); } else { graphics.drawBitmap(0, y+offsetY, icon.getWidth(), icon.getHeight(), icon, 0, 0); } mainScreenMenu.invalidate(index);
Also i tried removing and adding line by line. There's no memory-hogging problem until i add in the invalidate line.
Just as a test i tried creating another method but with drawing text instead of bitmaps. The same thing happened. This was the code i used:
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) { graphics.setFont(font); if(newsList.getSelectedIndex() == index) { graphics.setColor(0xCECEFF); graphics.fillRect(0,(index*newsList.getRowHeight()
), newsList.getWidth(), newsList.getRowHeight()); graphics.drawText(titles[index], 0, y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("")); graphics.setColor(0x000000); } graphics.drawText(titles[index], 0, y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("")); newsList.invalidate(index); }
Thanks,
Andrew
07-30-2009 11:30 AM
Hi,
Did you find a solution for this, I'm having the same problem here...
07-30-2009 11:59 AM
I run to a similar situation ( not trying to do exactly what you are doing), and the call for invalidate inside the drawListRow() will get you into a loop between invalidate and drawListRow(). So drawListRow() is called all the time.
What I did to solve the problem is to use the invalidate outside the drawListRow().
So in the screen class tha implement the ListFieldCallbackoverwrite the following methods:
protected boolean keyDown( int keycode, int status ){ invalidate(); return super.keyDown( keycode, status ); } protected boolean navigationMovement( int dx, int dy, int status, int time ){ invalidate(); return super.navigationMovement( dx, dy, status, time ); } protected int moveFocus(int amount, int status, int time){ invalidate(); return super.moveFocus(amount, status, time); }
Try this, it did it for me.
Rab
07-30-2009 12:20 PM
Hi, I did that but... I get "cannot find symbol" at invalidate(); and in the return..
private static class ListCallback implements ListFieldCallback {
private Vector listElements = new Vector();
protected boolean keyDown( int keycode, int status ){
invalidate();
return super.keyDown( keycode, status );
}
protected boolean navigationMovement( int dx, int dy, int status, int time ){
invalidate();
return super.navigationMovement( dx, dy, status, time );
}
protected int moveFocus(int amount, int status, int time){
invalidate();
return super.moveFocus(amount, status, time);
}
07-30-2009 12:25 PM
Those are methods of the MainScreen or FullScreen class that you use to display your list.
Somewhere you are creating the listField and adding it to a screen to be displayed, is this what you are doing ?
Rab