12-30-2011 03:18 PM
Hi ![]()
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 ![]()
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_HCENTE R|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);
}
}
Solved! Go to Solution.
12-30-2011 06:04 PM
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:
If you are codding for OS 6.0 and above, look at this:
Regarding centering vertically and horizontally, this is a pretty common requirement - search the forum for other Threads that have discissed this.
12-31-2011 01:10 AM
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 ??!
12-31-2011 04:40 AM
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 ![]()