Home‎ > ‎

No borders: Blackberry Bitmap ButtonField

posted Mar 26, 2011, 5:20 AM by Pieter-Bas IJdens   [ updated Mar 29, 2011, 11:06 PM ]
Lately I have been working on a Blackberry application with lots of UI customizations. One of the small hurdles I had to take was creating a button that merely display a bitmap. This is simple enough, but there are some tricky things having to do with rendering the borders. To save others the trouble of having to figure all this out themselves, here is the class:

Edit: Note that this class is for version 6 only. For v5, see http://www.ijdens.com/home/bitmapbuttonforblackberry5
 

package nl.sevensteps.blackberry.widgets;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.decor.BorderFactory;

public class BitmapButtonField extends ButtonField implements DrawStyle {
    private final Bitmap _noFocusBitmap;
    private final Bitmap _focusBitmap;
    private final Bitmap _disabledBitmap;
    private Bitmap _bitmap;

    BitmapButtonField(final Bitmap noFocus, final Bitmap focus,
            final Bitmap disabled) {
        super(Field.FOCUSABLE | ButtonField.CONSUME_CLICK);

        setLabel("");
        _noFocusBitmap = noFocus;
        _focusBitmap = focus;
        _bitmap = _noFocusBitmap;
        _disabledBitmap = disabled;

        // I don't care which visual state we're in, we do NOT want to draw a
        // border, ever.
        setBorder(VISUAL_STATE_FOCUS,
                BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_NORMAL,
                BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_ACTIVE,
                BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_DISABLED,
                BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_DISABLED_FOCUS,
                BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    }

    protected void onFocus(final int direction) {
        if (isEnabled()) {
            _bitmap = _focusBitmap;
        } else {
            _bitmap = _disabledBitmap;
        }
        invalidate();
    }

    protected void onUnfocus() {
        if (isEnabled()) {
            _bitmap = _noFocusBitmap;
        } else {
            _bitmap = _disabledBitmap;
        }
        invalidate();
    }

    public void setEnabled(final boolean enabled) {
        if (!enabled) {
            _bitmap = _disabledBitmap;
        }
        invalidate();
        super.setEnabled(enabled);
    }

    public int getPreferredWidth() {
        return _noFocusBitmap.getWidth();
    }

    public int getPreferredHeight() {
        return _noFocusBitmap.getHeight();
    }

    protected void layout(final int width, final int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());

    }

    protected void paint(final Graphics graphics) {
        graphics.drawBitmap(0, 0, _bitmap.getWidth(),
                _bitmap.getHeight(), _bitmap, 0, 0);
    }

    protected void applyTheme() {
        // Not applying the theme as this will cause the border to be rendered,
        // which we do not want to see, ever.
    }

    protected void applyTheme(final Graphics arg0, final boolean arg1) {
        // Not applying the theme as this will cause the border to be rendered,
        // which we do not want to see, ever.
    }
}


Enjoy!
Comments