customized button in Blackberry

For customizing the button with a background image which will come to focus while touching or pressing the button.

Just include this custom class:

public class CustomButtonImageField extends Field {

// private int backgroundColour = 0x100808;
 private int fieldWidth;
 private int fieldHeight;
 private int buttonHeight;
 private int buttonWidth;
 private String label = "";

private Font font;
 private int color;

Bitmap bmpBackground;
 Bitmap bmpFocussedButton, bmpUnFocussedButton;
 Bitmap buttonImage;

boolean bgImageActive = false;

public CustomButtonImageField(String label, Bitmap img, Bitmap unFocussedbutton, Bitmap focussedButton) {
 super(Field.FOCUSABLE);
 this.bmpBackground = img;
 this.bmpFocussedButton = focussedButton;
 this.bmpUnFocussedButton = unFocussedbutton;

this.font=FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 14);
 this.color=Color.WHITESMOKE;

buttonHeight = focussedButton.getHeight();
 buttonWidth = focussedButton.getWidth();
 this.label = label;
 fieldWidth = img.getWidth();
 fieldHeight = img.getHeight();
 buttonImage = unFocussedbutton;

this.setPadding(0, 0, 0, 0);
 }

public CustomButtonImageField(String label, Bitmap unFocussedbutton, Bitmap focussedButton, long style) {
 super(Field.FOCUSABLE | style);
 this.bmpBackground = null;
 this.bmpFocussedButton = focussedButton;
 this.bmpUnFocussedButton = unFocussedbutton;
 this.label = label;
 this.font=FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 14);
 this.color=Color.WHITESMOKE;

buttonHeight = focussedButton.getHeight();
 buttonWidth = focussedButton.getWidth();
 this.label = label;
 fieldHeight = focussedButton.getHeight();
 fieldWidth = focussedButton.getWidth();
 buttonImage = unFocussedbutton;

this.setPadding(0, 0, 0, 0);
 }

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

protected void onFocus(int direction) {

VirtualKeyboard virtualkeybrd = UiApplication.getUiApplication().getActiveScreen().getVirtualKeyboard();

if (virtualkeybrd != null)
 {
 virtualkeybrd.setVisibility(VirtualKeyboard.HIDE);
 }
 buttonImage = bmpFocussedButton;

invalidate();
 }

protected void onUnfocus() {
 buttonImage = bmpUnFocussedButton;
 invalidate();
 }

public int getPreferredWidth() {
 return fieldWidth;
 }

public int getPreferredHeight() {
 return fieldHeight;
 }

protected void layout(int arg0, int arg1) {
 setExtent(getPreferredWidth(), getPreferredHeight());
 }

protected void drawFocus(Graphics graphics, boolean on) {

}

public void setFocussedImage(Bitmap focusImage) {
 bmpFocussedButton = focusImage;
 }

public void setBackgroundImage(Bitmap bgImage) {

buttonImage = bgImage;
 invalidate();

}
 public void setBgImgStatus(boolean status) {

bgImageActive = status;

}
 public boolean isBgImageActive() {

return bgImageActive;

}

protected void fieldChangeNotify(int context) {
 try {
 this.getChangeListener().fieldChanged(this, context);
 } catch (Exception e) {
 }
 }

protected void paint(Graphics graphics) {
 if (bmpBackground != null) {
 graphics.drawBitmap(0, 0, bmpBackground.getWidth(), bmpBackground.getHeight(), bmpBackground, 0, 0);

} else {
 graphics.drawBitmap(0, 0, buttonImage.getWidth(), buttonImage.getHeight(), buttonImage, 0, 0);
 }
 graphics.setFont(font);
 graphics.setColor(color);

if (font == BaseScreen.FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 20) && label.indexOf('@')>0){
 graphics.drawText(label, 40, buttonHeight / 2, graphics.HCENTER | graphics.VCENTER, buttonWidth);// 0,7,
 }else {
 graphics.drawText(label, 0, buttonHeight / 2, graphics.HCENTER | graphics.VCENTER, buttonWidth);// 0,7,
 }

}
 public void setColorAndFont(Font font, int color){
 this.font=font;
 this.color=color;
 }

public void setImages(Bitmap focussedButton){
 buttonImage = focussedButton;
 fieldWidth = buttonImage.getWidth();
 fieldHeight = buttonImage.getHeight();
 invalidate();
 }
 }

Then use this button in your screen like below:

btncSubmit = new CustomButtonImageField("Login", Bitmap.getBitmapResource("unfocus_btn.png"), Bitmap.getBitmapResource("focus_button.png"), Field.FIELD_LEADING);
 btncSubmit.setChangeListener(this);

Hope it will help.  Cheers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s