Set focus color on clickable array of HorizontalFieldManager in BlackBerry

129 Views Asked by At

I have an array of horizontal fields which contains a bitmap and a labelfield each. The whole row should be clickable which is working so far, but how can I set the focus color properly? At the moment the onFocus and onUnfocus functions are being completely ignored.

This is the definition of my array:

for (int i = 0; i < listSize; i++) {
        logInDetailManager[i] = new HorizontalFieldManager(
                Manager.USE_ALL_WIDTH | Field.FOCUSABLE) {

            protected void onFocus(int direction) {
                super.onFocus(direction);
                background_color = Color.RED;
                invalidate();
            }

            protected void onUnfocus() {
                invalidate();
                background_color = Color.GREEN;
            }

And this is how I add my horizontal fields:

logInDetailManager[i].setChangeListener(this);
logInDetailManager[i].add(dummyIcon[i]);
logInDetailManager[i].add(new LabelField("hello"));
logInDetailManager[i].add(new NullField(Field.FOCUSABLE));
add(logInDetailManager[i]);
1

There are 1 best solutions below

0
On

Sorry, I couldn't comment to my own post yesterday since I'm new to Stackoverflow ;) Here's how I solved it:

I removed onFocus() and onUnfocus() from the HFM and set the background color in the paint method so the whole row color is changed when focused:

 protected void paint(Graphics graphics) {

        graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
        graphics.clear();
        invalidate();
        super.paint(graphics);
}

I also found out that if you want to set more complex backgrounds (i.e. with a gradient) you can also use the setBackground(int visual, Background background) method:

 Background bg_focus = (BackgroundFactory
        .createLinearGradientBackground(Color.GREEN, Color.LIGHTGREEN,
        Color.LIGHTGREEN, Color.GREEN));

 loginDetailManager[i].setBackground(VISUAL_STATE_FOCUS, bg_focus);

Make sure to delete you're paint method when using the setBackground function like that!