BlackBerry customized HorizontalFieldManager

1.2k views Asked by At

In Blackberry I want to create a class that extends HorizontalFieldManager so that I can display a label and an image on the same line with the label to the left and the image to the right.
I want the user to be able to interact with the HorizontalFieldManager as if it were a single field. I also want each HorizontalFieldManager to be focusable when added to a VeriticalFieldManager. I also want the clcik action events.

1

There are 1 answers

0
Jonathan Fisher On

Sounds like you'll be wanting to start writing your own Field classes, here's an example to get you started:


import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class SimpleField extends Field {

    private String label;
    private Bitmap image;
    private int fieldWidth;
    private int fieldHeight;
    private boolean hover = false;
    private int focusColor = 0xcccccc;

    public SimpleField(String label, Bitmap image) {
        super(Field.FOCUSABLE);
        this.label = label;
        this.image = image;
        fieldWidth = Display.getWidth();
        fieldHeight = image.getHeight();
    }

    protected void onFocus(int direction) {
        hover = true;
        invalidate();
        super.onFocus(direction);
    }

    protected void onUnfocus() {
        hover = false;
        invalidate();
        super.onUnfocus();
    }

    public int getPreferredWidth() {
        return fieldWidth;
    }

    public int getPreferredHeight() {
        return fieldHeight;
    }

    protected void layout(int width, int height) {
        setExtent(fieldWidth, fieldHeight);
    }

    protected void paint(Graphics graphics) {
        if(hover){
            graphics.setColor(focusColor);
            graphics.fillRect(0, 0, fieldWidth, fieldHeight);
        }
        graphics.drawText(label, 0, (fieldHeight - graphics.getFont().getHeight()) / 2);
        graphics.drawBitmap(graphics.getFont().getAdvance(label), 0, image.getWidth(), image.getHeight(), image, 0, 0);
    }
}