Tuesday, January 11, 2011

How to create a Custom Button Field in Blackberry

Hello all,
This is my first blog.I will be happy if it can help blackberry developers in any way.

Custom Buttons :  Blackberry supports buttons that a very simple yet use full which is by default dark grey in color and when it gets focus then it appears in blue color.

Most of the time we want to create apps or projects with a wide variety of graphics in it that can appeal the user.

For this purpose we can create our own custom buttons and apply focusable and non focusable images on it.Moreover we can also add text to it.

Here u go:

package com.MyProject;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;

public class CustomButtonField extends Field
{
    Bitmap Unfocus_img, Focus_img, current_pic;
    int width;
    String text;
    Font font;   
    CustomButtonField(int width, String text, Bitmap onFocus, Bitmap onUnfocus, long style)
    {
        super(style);
        Unfocus_img = onUnfocus;
        Focus_img = onFocus;
        current_pic = onFocus;
        this.text = text;
        this.width = width;
    }
    protected void layout(int width, int height)
    {
        setExtent(current_pic.getWidth(), current_pic.getHeight());       
    }
    protected void paint(Graphics graphics)
    {
        try
        {
                FontFamily fntFamily = FontFamily.forName("BBAlpha Sans");
                font = fntFamily.getFont(Font.BOLD,14);             
        }
        catch(Exception e)
        {
            font = Font.getDefault();
         
        }
        graphics.setFont(font);
        graphics.setColor(Color.WHITE);
        graphics.drawBitmap(0, 0, current_pic.getWidth(), current_pic.getHeight(), current_pic, 0, 0);
        graphics.drawText(text, width, 7);
    }
    protected void onFocus(int direction)
    {
        super.onFocus(direction);
        current_pic = Unfocus_img;
        this.invalidate();
    }
  protected void drawFocus(Graphics graphics, boolean on)
  {
       
    }
    protected void onUnfocus()
    {
        super.onUnfocus();
        current_pic = Focus_img;
        invalidate();
    }
    public boolean isFocusable() {
        return true;
    }
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }
}


And this is how u can use it:
Bitmap focus = Bitmap.getBitmapResource("printButton_focus.png");
Bitmap unfocus = Bitmap.getBitmapResource("printButton_unfocus.png");
           
Bitmap focus1 = Bitmap.getBitmapResource("button-highlight.png");Bitmap unfocus1 = Bitmap.getBitmapResource("button.png");
           
 ButtonField obj = new ButtonField("Button");
 CustomButtonField button1 = new CustomButtonField(0,"",unfocus,focus,Field.FOCUSABLE);
 CustomButtonField button2 = new CustomButtonField(25,"Next",unfocus1,focus1,Field.FOCUSABLE);
           
 add(obj);
 add(new RichTextField(Field.NON_FOCUSABLE));
 add(button1);
 add(new RichTextField(Field.NON_FOCUSABLE));
 add(button2);


Result of three added buttons depending upon the argument
Unfocus  

Focus    

Unfocus 
   
Focus    

Unfocus

Focus   


Thank you to all those who helped me!!!!

4 comments:

  1. Thanks a lot, kudos to your effort, neat material!!

    ReplyDelete
  2. nice article but , you miss the , process behind , the custom focus button .

    ReplyDelete
  3. hello, how to get the text into the middle of button? my text is left top button not in the middle...

    ReplyDelete
  4. swati give me ur email id i have an one que. about blackberry developement...login fb into bb.pz hlp me

    ReplyDelete