0

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);
    }
}
iAhmad
  • 3
  • 2
  • if want your ButtonField in center, vertically and horizontally, you have to implement a Custom FieldManager which will place ButtonField instances as you want. Your BitmapButtonField class seems okay, only the width of the bitmap will cause some problem if it exceeds maximum allowable width provided by the parent manger. – Rupak Jan 01 '12 at 14:46

1 Answers1

0

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.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114