Alan Lai
Alan Lai

Reputation: 1104

Blackberry Custom ButtonField

This is default look when i launch activity.

After i play around with the wheel track pad then become like this. The button image or whole button lost.

Here is my custom buttonfield which extends ButtonField.

public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;

int mWidth;
int mHeight;

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

Upvotes: 0

Views: 294

Answers (2)

Suresh Kerai
Suresh Kerai

Reputation: 921

Set Focusable on constructor.

super(Field.FOCUSABLE);

Upvotes: 0

donturner
donturner

Reputation: 19166

It looks like you're trying to create a button with an image so you might be better off using the Advanced UI Library published by RIM. Take a look at the BitmapButtonField class.

Upvotes: 1

Related Questions