Create Custom Blinking Button with Blackberry SDK


This is a simple code I have writen as Blackberry Developer.
Altough this article is tagged under Java Category, but this article work on Blackberry SDK.
You can change the background color of Button by customize the extending the ButtonField Class. Below is a sample of customizing ButtonField. This Button will blink when you click it and will stop blinking after you click it again:


package puji.padi.PjiDakon2;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;

public class BlinkingButton extends ButtonField{
int width;
int height;
String _text;
int padding = 5;
int _backgroundcolor;
public boolean blinking;
BlinkingButton(String text){
super(text,ButtonField.CONSUME_CLICK);
Font font = Font.getDefault();
_text = text;
width = getPreferredWidth();
height = padding + font.getHeight()+ padding;
height=super.getHeight()+padding*2;
height=getPreferredHeight();
_backgroundcolor = Color.AQUA;
blinking=false;
}
protected void layout(int a, int b){
setExtent(width, height);
}
public void paint(Graphics g){
g.setColor(Color.BISQUE);
g.setColor(_backgroundcolor);
g.drawText(_text, 0, 0);
}
public void setBackgroundColor(int color){
_backgroundcolor = color;
invalidate();
}
public void setLabel(String text){
_text = text;
invalidate();
}
public String getLabel(){
return _text;
}
public void blink(){
blinking=!blinking;
Thread trblink = new Blinky();
trblink.start();
}
class Blinky extends Thread{
public void run(){
while(blinking){
_backgroundcolor = Color.GOLD;
invalidate();
try{
Thread.sleep(200);
}
catch(Exception e){

}
_backgroundcolor = Color.BROWN;
invalidate();
try{
Thread.sleep(200);
}
catch(Exception e){

}
}
}
}
}

the overall code will be like this:


package puji.padi.PjiCustomButton2;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class MainClass extends UiApplication{
MainClass(){
HomeScreen home=new HomeScreen();
pushScreen(home);
}
public static void main(String[] args){
MainClass app=new MainClass();
app.enterEventDispatcher();
}
class HomeScreen extends MainScreen{
HomeScreen(){
BlinkingButton bb = new BlinkingButton("Click to Blink");
add(bb);
bb.setChangeListener(listener);
}
FieldChangeListener listener = new FieldChangeListener(){
public void fieldChanged(Field field, int context){
BlinkingButton bb = (BlinkingButton)field;
bb.blink();
}
};
}
}

BlinkingButton
This will result a Blackberry Button. It will be blinking when you click, and will be stopped when you click again.
Feel free to post your comment …

About abunajiyah
A Programmer

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

%d bloggers like this: