Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
jlaprise
Posts: 2
Registered: ‎03-10-2009
My Device: Not Specified
Accepted Solution

Customizing the background color for a ListField item

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?

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Customizing the background color for a ListField item

Try something like this.

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.

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
New Developer
jlaprise
Posts: 2
Registered: ‎03-10-2009
My Device: Not Specified

Re: Customizing the background color for a ListField item

One other thing I figured out in trying to get this to work is getSelectedIndex() returns 0 when nothing is selected. I used isFocus() to determine focus on the ListField and then set the selectedIndex to -1. This resolves the issue of the first row being highlighted by default.
Please use plain text.
Developer
andd64
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: Customizing the background color for a ListField item

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

 

Please use plain text.
Developer
Rajat_10Sep
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: Customizing the background color for a ListField item

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.

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Please use plain text.
Developer
andd64
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: Customizing the background color for a ListField item

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

Please use plain text.
Developer
RealCaracas
Posts: 15
Registered: ‎07-30-2009
My Device: Not Specified

Re: Customizing the background color for a ListField item

Hi,

 

Did you find a solution for this, I'm having the same problem here... 

Please use plain text.
Developer
Developer
rab
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: Customizing the background color for a ListField item

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

Please use plain text.
Developer
RealCaracas
Posts: 15
Registered: ‎07-30-2009
My Device: Not Specified

Re: Customizing the background color for a ListField item

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

            } 

Please use plain text.
Developer
Developer
rab
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: Customizing the background color for a ListField item

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

Please use plain text.