wtorek, 9 października 2012

Blackberry Custom Checkbox

Blackberry platform offers only basic widgets.
In this thread I will start quick examples of custom widgets for a blackberry 6/7. F
For a good starting point for component development bb developers may also use AdvancedUIExample project provided by RIM.

Following example presents simple check-box with description text below this field.
Widget was inspired by android platform.

public class CheckboxWithDescription extends CheckboxField {
   
    // Factor for extending field height 
    private static final double factor = 1.7;
    
    // Additional description
    private String description;

    // Style for placement of checkbox in right.
    private final static long LEFT_LABEL_STYLE = 134217728;

 
    public CheckboxWithDescription(String label, String description, boolean checked, long style) {
        super(label, checked, style | LEFT_LABEL |USE_ALL_WIDTH);
        this.description = description;
    }

    protected void layout(int width, int height) {
        super.layout(width, height);
        setExtent(width, (int)(getHeight() * factor));
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        int heightAnchor = (int) (getHeight() / 1.8);
        Font font = graphics.getFont();
        graphics.setFont(font.derive(Font.BOLD, font.getHeight(Ui.UNITS_pt) - 1, Ui.UNITS_pt));
        graphics.drawText(description, font.getBounds(getLabel()), heightAnchor);
    }
}

This example draws standard check-box field and extend it's height to make place for description. A paint method can be extended to provide different colour of description etc. Any comments are appreciated. Example tested with blackberry 7.0