Pradeep's Blog

July 14, 2010

ButtonField with Image as a BackGround

Filed under: BlackBerry Sample Code — Pradeep @ 11:39 pm

public class CustomSmallButton extends Field {
private Bitmap offPicture;
private Bitmap onPicture ;

public CustomSmallButton(Bitmap onFocous, Bitmap offFocous, long style) {
super(style);
onPicture = onFocous;
offPicture = offFocous;
this.setPadding(5,5, 5, 5);
}

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

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

protected void layout(int width , int height) {
setExtent(Math.min(width, getPreferredWidth()), Math.min(height,getPreferredHeight()));
}

protected void drawFocus(Graphics graphics, boolean on) {
// Don’t draw the default focus
}

protected void paint(Graphics graphics) {
/*When button is on focus*/
if (isFocus()) {
graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0);
} else {
/*When button is off focus*/
graphics.drawBitmap(0, 0, getWidth(), getHeight(),offPicture , 0, 0);
}
}

public boolean isFocusable() {
return true;
}

protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}

protected void onUnfocus() {
super.onUnfocus();
invalidate();
}

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}

protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}

1 Comment »

  1. Thank You Very much …

    Comment by predhin — January 4, 2011 @ 2:37 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.