Thursday, May 3, 2012

Create UI components in Blackberry

In Blackberry Field class is most use full class. There are lot of subclass of Field class also if you want to create your custom GUI field then you have to use that class. Today's i have write about simple example of Filed subclass like ButtonField, CheckboxField, DateField ,NumbericChoiceField, ObjectChoiceField,RadioButtonField,RichTextField, BasicEditFiled, EditField, PasswordEditField, AutoTextEditField and LableField etc.

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.component.AutoTextEditField;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NumericChoiceField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.RadioButtonField;
import net.rim.device.api.ui.component.RadioButtonGroup;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class FieldSubClassDemo extends MainScreen{
 public FieldSubClassDemo(){
  
  //add Bitmap Field  
  BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("img/icon.png"));
  add(bitmapField);
  
  //Create Button
  ButtonField btn = new ButtonField("Test Button");
  add(btn);
  
  //Create Numeric Drop-down list
  NumericChoiceField numChoice = new NumericChoiceField("Slect Number",1,10,1);
  add(numChoice);
  
  //Create Alphanumeric drop-down List. 
  String choiceItem[] = {"India","UK","US","Canada"};
  add(new ObjectChoiceField("Country: ", choiceItem));
  
  //Create Check Box
  CheckboxField checkBox=  new CheckboxField("Are you marriage?",false);
  add(checkBox);
  
  //Create RadioButton
  RadioButtonGroup sexGroup = new RadioButtonGroup();
  RadioButtonField choMale =new RadioButtonField("Male");
  RadioButtonField choFemale =new RadioButtonField("Female"); 
  sexGroup.add(choMale);
  sexGroup.add(choFemale);
  add(choMale);
  add(choFemale);
  
  //Create Date Field
  DateField df = new DateField("Brithday: ",System.currentTimeMillis(),DateField.DATE);
  add(df);
  
  //Create RichText Field
        RichTextField rtField;
        //An array of fonts to be passed to the RichTextField.
        Font fonts[] = new Font[3];

        //A list of offsets of character positions of the string contained in the RichTextField where font appearance will change.
        int[] offset = new int[4];

        //An array of indexes that align with the offset values and indicate
        //the index of the font array of the font to use starting at an offset.
        byte[] attribute = new byte[3];

        //Get three instances of the default font.
        //On plain, one bold and one bold and italic.
        fonts[0] = Font.getDefault();
        fonts[1] = Font.getDefault().derive(Font.BOLD);
        fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC);

        //The string of text we will display in the RichTextField.
        String richText = "This is how you create text with formatting!!!";

        //Offset will start a character 0 of the string.
        offset[0] = 0;
        //At character position 0 we want to use the font at
        // position 2 of the font array (bold and italic).
        attribute[0] = 2;

        //Change to the default (non-bold, non-italic) font at
        //the 4th character position.
        offset[1] = 4;
        attribute[1] = 0;

        //Change to bold font at the 33rd character position.
        offset[2] = 33;
        attribute[2] = 1;

        //Add the last character position to the offset.
        offset[3] = richText.length();

        //Instantiate rtField.
        rtField = new RichTextField
          (richText, offset, attribute, fonts,
           RichTextField.USE_TEXT_WIDTH);

        //Add the RichTextField to the MainScreen.
        add(rtField);
        
        //BasicEdit Field
        BasicEditField beField = new BasicEditField("UserName: ","");
        add(beField);
        
        //Password Field
        PasswordEditField pwField = new PasswordEditField("Password: ","");
        add(pwField);
        
        AutoTextEditField autoTxtField = new AutoTextEditField("AutoTextField: ","");
        add(autoTxtField);
        
        //create GaugeField
        GaugeField gaugeField = new GaugeField("Gauge", 0, 100, 29, GaugeField.PERCENT);
        add(gaugeField);
        
        //Create Lable Field
        LabelField lblField = new LabelField("Sample Label");
        add(lblField);
 }
}

1 comment: