Monday, February 13, 2012

Color Label Field in Blackberry

In Blackberry some time user need to display colored text instated on simple black color text. Here sample code that use full for how to create color LabelField. We can also change label Field background color as well Field Corner.

1. Change Text Color
LabelField labelField = new LabelField("Color Label Field"){
     protected void paint(Graphics graphics) {
      graphics.setColor(Color.BLUE);
      super.paint(graphics);
 }
};


Following output will display


2. Change Lable Field Background and text color.
LabelField labelField = new LabelField("Color Label Field"){
     protected void paint(Graphics graphics) {
        graphics.setColor(Color.BLUE);//Set Background color
 graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 0, 0);//Fill that color on Background
 graphics.setColor(Color.RED);//set text color now here we set RED 
 super.paint(graphics);
     }
};
Following output will display

3. Change Label Field Background, text color, rounded corner.

LabelField labelField = new LabelField("Color Label Field "){
       protected void paint(Graphics graphics) {
 graphics.setColor(Color.BLUE);//Set Background color
 graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 7, 7);//Fill that color on Background
 graphics.setColor(Color.RED);
 super.paint(graphics);
       }
   
};
Following output will display

4. Color Label Field with focusable and change color on focus got and focus lost.

  LabelField labelField2 = new LabelField("Color Label Field",Field.FOCUSABLE){
   
   boolean _inFocus = false;
      public void onFocus(int direction) {
          _inFocus = true;
          this.invalidate();
      }
      public void onUnfocus() {
          _inFocus = false;
          this.invalidate();
      }
       protected void paint(Graphics graphics) {
        if(_inFocus){
         graphics.setColor(Color.BLUE);
        }else{
         graphics.setColor(Color.RED);
        }
        super.paint(graphics);
   }
       protected void drawFocus(Graphics g, boolean on) {
       }
  };
out put Like.


Thank you.



1 comment: