Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
Ahmad89
Posts: 26
Registered: 10-24-2011
My Carrier: NoN
Accepted Solution

Buttons with images, fixing the size ?

Hi :smileyhappy:

 

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 ??

 

And Finally, is there any way to make the default selected button as NULL ? So, button 1 is not selected at the start as shown on the attached image !!

 

*Image attached*

 

Thanks in advance :smileyhappy:

 

The code:

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);
        }
}

 

Please use plain text.
Developer
peter_strange
Posts: 14,614
Registered: 07-14-2008

Re: Buttons with images, fixing the size ?

I think you have given us the wrong code to look at.

 

Where buttons are positioned is determined by the Manager the Buttons are added into. 

 

The only control the buttons have is their size - and you control that in this case by the size of the image you use when you create the buttons.  So perhaps you nbeed to scale the button images you give the buttons to make sure they fit on the screen.

 

Regarding arranging these in rows, if you are coding for OS 5.0 have a look at this:

http://supportforums.blackberry.com/t5/Java-Development/Create-a-rich-UI-layout-with-TableLayoutMana...

 

If you are codding for OS 6.0 and above, look at this:

http://supportforums.blackberry.com/t5/Java-Development/Using-TableModel-TableView-TableController-a...

 

Regarding centering vertically and horizontally, this is a pretty common requirement - search the forum for other Threads that have discissed this. 

Please use plain text.
Developer
Ahmad89
Posts: 26
Registered: 10-24-2011
My Carrier: NoN

Re: Buttons with images, fixing the size ?

Okay, sorry about the wrong code. I thought the given code should be enough !

 

Here is the code for the FieldManager:

 

 

.
.
.
GridFieldManager Grid = new GridFieldManager(5,3,0);
.
.
.
Grid.add(BUTTONS);
add(Grid);
.
.
.

 

Isn't there any code to stop it from getting outside the screen rather than scalling the images ??!

Please use plain text.
Developer
Ahmad89
Posts: 26
Registered: 10-24-2011
My Carrier: NoN

Re: Buttons with images, fixing the size ?

SOLVED

 

I've solved it by:

 

1- rescaling the images with the display window:

        public int getPreferredWidth() {
                return Display.getWidth()/6;
        }

        public int getPreferredHeight() {
                return Display.getHeight()/6;
        }

 

2- Using HorizontalFieldManager:

HorizontalFieldManager(FIELD_HCENTER);

 

 

Thanks :smileyhappy:

Please use plain text.