views:

401

answers:

2

How to set image as a background for ButtonField in BlackBerry?

+1  A: 

I don't believe you can set an image to the ButtonField. Instead, you can extend the BitmapField class, override the trackwheelClick function and use the onFocus to determine if it was this field that was clicked. This will give an image that is "clickable".

Tamar
+2  A: 

Other way is to extend ButtonField and draw image on paint:

class BitmapButtonField extends ButtonField {
    Bitmap mNormal;
    Bitmap mFocused;
    Bitmap mActive;

    int mWidth;
    int mHeight;

    public BitmapButtonField(Bitmap normal, Bitmap focused, 
        Bitmap active) {
        super(CONSUME_CLICK);
        mNormal = normal;
        mFocused = focused;
        mActive = active;
        mWidth = mNormal.getWidth();
        mHeight = mNormal.getHeight();
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, 0);
        setBorder(BorderFactory
                        .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_ACTIVE, BorderFactory
                        .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    }

    protected void paint(Graphics graphics) {
        Bitmap bitmap = null;
        switch (getVisualState()) {
        case VISUAL_STATE_NORMAL:
                bitmap = mNormal;
                break;
        case VISUAL_STATE_FOCUS:
                bitmap = mFocused;
                break;
        case VISUAL_STATE_ACTIVE:
                bitmap = mActive;
                break;
        default:
                bitmap = mNormal;
        }
        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                        bitmap, 0, 0);
    }

    public int getPreferredWidth() {
        return mWidth;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    protected void layout(int width, int height) {
        setExtent(mWidth, mHeight);
    }
}

sample of use

Max Gontar