In Blackberry Java application development, every row in ListField has the default blue focus highlight color. There is no simple setter to change it. One way to change it is to overwrite the 'drawFocus' method. I'm going to walk you through the steps to change that.
Suppose we have a list to display each news feed from an RSS; we need to customize the row so that it looks like this:
In the non-focus state, the title of the news feed is in black, the date info is in gray, and the background color for the row is white. For the focus state the colors will be changed: the row gets a red highlight color and the title/date fields will get white. Here is the code:
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(Color.RED); //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); } } finally { graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus); } }
The idea is to draw the customized highlight color and then draw the selected row again. The drawListRow method will draw each row; suppose we have the title field’s paint method like this:
protected void paint(Graphics g) { try { g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? textFocusColor : textUnFocusColor); g.drawText(__line1, PADDING_LEFT, PADDING_TOP); if (__line2 != null) { g.setDrawingStyle(DrawStyle.ELLIPSIS, true); g.drawText(__line2, PADDING_LEFT, PADDING_TOP + _font.getHeight()); } } finally { if (__line2 != null) { g.setDrawingStyle(DrawStyle.ELLIPSIS, false); } } }
Here the title takes 2 lines at most. And the paint method will be invoked by its manager from the drawListRow method. Check back for more Blackberry tips on our blog in the future!