Question

I'm using the following code to add images to the buttons. However, I got some of the buttons out of the window view !! What should I add to make them fit on the view window ?!

PLUS: I want them to be on the center, vertically and horizontally. Let's say that I have 15 buttons, I want 3 each row. And in the center of the screen ??

Thanks in advance

public class BitmapButtonField extends ButtonField {
    private Bitmap bitmap;
    private Bitmap bitmapHighlight;
    private boolean highlighted = false;

    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight) {
        this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER);
    }

    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight, long style) {
        super(style);
        this.bitmap = bitmap;
        this.bitmapHighlight = bitmapHighlight;
    }

    protected void layout(int width, int height) {
            setExtent(getPreferredWidth(), getPreferredHeight());
    }

    public int getPreferredWidth() {
            return bitmap.getWidth();
    }

    public int getPreferredHeight() {
            return bitmap.getHeight();
    }

    protected void paint(Graphics graphics) {
            super.paint(graphics);
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Bitmap b = bitmap;
            if (highlighted)
                b = bitmapHighlight;
            graphics.drawBitmap(0, 0, width, height, b, 0, 0);
    }
}
Was it helpful?

Solution

There is nothing wrong with your field. Most probably you're using HorizontalFieldManager which has infinite width and places fields out of screen bounds. You can use FlowFieldManager to change this behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top