how to set layout background image in blackberry - blackberry

I am trying to set VerticalFieldManager background image. here is my code hope it would help you to find problem.
my problem is
1) background image is appear but not get full height of the image.
VerticalFieldManager verFM = new VerticalFieldManager(USE_ALL_WIDTH );
Bitmap bt = Bitmap.getBitmapResource("top_bar#2x.png");
BitmapField btm = new BitmapField(bt);
btm.getPreferredHeight();
verFM.add(btm);
VerticalFieldManager VFM = new VerticalFieldManager( USE_ALL_HEIGHT);
VFM.setMargin(5, 5, 10, 40);
VFM.setPadding(5, 5, 60, 10);
System.out.println();
Bitmap bitmap = Bitmap.getBitmapResource("registration_form_bg#2x.png");
Background bgg = BackgroundFactory.createBitmapBackground(bitmap,Background.POSITION_X_LEFT, Background.POSITION_Y_TOP,Background.REPEAT_SCALE_TO_FIT);
VFM.setBackground(bgg);
verFM.add(VFM);
add(verFM);

Related

Set width and height on BlackBerry BitmapField

How do I set width and height of a BitmapField in BlackBerry? The following code works, but I can't show the entire image. The display shows only some portion of my image.
BitmapField bmp = new BitmapField(connectServerForImage(ImageUrl)) {
protected void layout(int width, int height)
{
setExtent(80, 70);
}
};
I assume you're saying that your BitmapField is showing the image you retrieve cropped?
And you would like it to scale, perhaps scaled to fit the size of your BitmapField? There's many ways to do that, but one is to resize the image after downloading, before giving it to the BitmapField:
From this answer on resizing a Bitmap
public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
Bitmap newImage = new Bitmap(newWidth, newHeight);
originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
return newImage;
}
(Note that the link I provided has an alternate method for resizing images, if you have to support BlackBerry OS < 5.0).
Then, use
Bitmap img = resizeImage(connectServerForImage(ImageUrl), 80, 70);
BitmapField bmp = new BitmapField(img) {
protected void layout(int width, int height)
{
setExtent(80, 70);
}
};
However, I believe that if you actually set the size of the Bitmap img to 80x70, then there's no need to also set the extent of the BitmapField to 80x70. It should default to the size of the image you give it. So, you could simplify the code to:
BitmapField bmp = new BitmapField(img);
int prefWidth=(30*bitmap.getWidth())/bitmap.getHeight();
Bitmap temp=resizeBitmap(bitmap,prefWidth,30);
where
private Bitmap resizeBitmap(Bitmap image, int width, int height)
{
int rgb[] = new int[image.getWidth()*image.getHeight()];
image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
This support bb os 4.5

Center aligning a CheckboxField

I have a CheckboxField that is center aligned using the code below:
reminderCheckbox = new CheckboxField(res.getString(OPT_OUT_REMINDER_OFF), false, Field.FIELD_HCENTER);
reminderCheckbox.setPadding(getRelativePixels(10), 0, getRelativePixels(15), 0);
add(reminderCheckbox);
private int getRelativePixels(int size) {
int displaySize = Display.getWidth();
if (displaySize == 480)
return size;
double relativeSize = (double) size / 480.0;
return (int) (relativeSize * displaySize);
}
getRelativePixels() is just to adjust the padding on different resolutions.
My problem is, the CheckboxField is appearing in the center on the simulator but is left aligned on the phone (Bold 9700, running OS 5)
Any advice would be appreciated.
Try like this
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
reminderCheckbox = new CheckboxField(res.getString(OPT_OUT_REMINDER_OFF), false);
hfm.add(reminderCheckbox);
add(hfm);

how to set an image as background in blackberry

I was able to add the background only to the fields that were added in the screen with the below code:
VerticalFieldManager manager = new VerticalFieldManager();
Bitmap image = Bitmap.getBitmapResource("Penguins.jpg");
Background bg = BackgroundFactory.createBitmapBackground(image);
manager.setBackground(bg);
manager.add(new LabelField("HEll"));
add(manager);</code>
the output screen is like below:
How will get the image filled with whole screen, with the fields visible on top of it. Thank you
Bitmap background = Bitmap.getBitmapResource("background.png");
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public void paint(Graphics g) {
g.drawBitmap(0, 0, "Device Width", "Device Height",
background, 0, 0);
super.paint(g);
}
};
vfm.add(new LabelField("HEll"));
add(vfm);
You can also use this code to set the background image in Blackberry, But this works only 5.0 or above.
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap);
// OR
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap, int positionX, int positionY, int repeat);
this.getMainManager().setBackground(bg);

How to change Background of HorizontalFieldManager

I want to set the Background of HorizontalFieldManager.
The sample code I've been searching is setting the background using Gradient for the main screen background.
//create gradient linear for background
this.getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(0x0099CCFF,
0x0099CCFF,0x00336699,0x00336699)
);
Then I try to use the same pattern to set background to HorizontalFieldManager, since it have this method. But it won't work. Here's the code
HorizontalFieldManager hManager = new HorizontalFieldManager();
Bitmap bitmapImage = null;
bitmapImage = Bitmap.getBitmapResource("img/home.png");
tabHome = new BitmapField(bitmapImage, BitmapField.FOCUSABLE
| BitmapField.HIGHLIGHT_FOCUS);
bitmapImage = Bitmap.getBitmapResource("img/in.png");
tabCheckInOut = new BitmapField(bitmapImage, BitmapField.FOCUSABLE
| BitmapField.HIGHLIGHT_FOCUS);
bitmapImage = Bitmap.getBitmapResource("img/barcode.png");
tabBarcode = new BitmapField(bitmapImage, BitmapField.FOCUSABLE
| BitmapField.HIGHLIGHT_FOCUS);
bitmapImage = Bitmap.getBitmapResource("img/options.png");
tabOptions = new BitmapField(bitmapImage, BitmapField.FOCUSABLE
| BitmapField.HIGHLIGHT_FOCUS);
tabHome.setFocusListener(this);
tabCheckInOut.setFocusListener(this);
tabBarcode.setFocusListener(this);
tabOptions.setFocusListener(this);
Background topBack = BackgroundFactory.createSolidBackground(0x00606A85);
hManager.setBackground(topBack);
hManager.add(tabHome);
hManager.add(tabCheckInOut);
hManager.add(tabBarcode);
hManager.add(tabOptions);
add(hManager);
I'm using HorizontalFieldManager and add 4 BitmapField, then I using BackgroundFactory to create solidBackground, and set it to the manager
but when I run it, the background color does not apply. The gradient example work well. Is there anything that I'm missing? Please help me.
Thanks
After doing some deep web search. Here's the answer guys
HorizontalFieldManager manager = new HorizontalFieldManager()
{
public void paint(Graphics graphics)
{
graphics.setBackgroundColor(0x000000FF);//blue
graphics.clear();
super.paint(graphics);
}
};
UPDATE:
You must only use Web Color
such as 0x006699FF should work
but 0x00606A85 would not work.
if you want a specific color, i recommend you using bitmap.
UPDATE:
Another solution
HorizontalFieldManager manager = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
manager.setBackground(BackgroundFactory.BackgroundFactory
.createSolidBackground(0x00cccccc));

How do you set the background image in a BlackBerry application using Java?

how i set background image in java blackberry simulator ?
backgroundBitmap = Bitmap.getBitmapResource("background.png");
MainScreen mainScreen = new MainScreen();
HorizontalFieldManager horizontalFieldManager =
new HorizontalFieldManager(
HorizontalFieldManager.USE_ALL_WIDTH |
HorizontalFieldManager.USE_ALL_HEIGHT){
//Override the paint method to draw the background image.
public void paint(Graphics graphics) {
//Draw the background image and then call paint.
graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
try this blog post:
How to set Background Image in Blackberry
Or this support forum thread:
drawing bitmap in Mainscreen background
Bitmap bitmapOrig = Bitmap.getBitmapResource("ICON.png");
// Create a Bitmap of arbitrary size
int scaledX = 360;
int scaledY = 415;
Bitmap bitmapScaled = new Bitmap(scaledX, scaledY);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_LANCZOS);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled , Field.FOCUSABLE);
homeScreen.add(bitmapFieldScaled2);
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(
HorizontalFieldManager.USE_ALL_WIDTH |
HorizontalFieldManager.USE_ALL_HEIGHT){
//Override the paint method to draw the background image.
public void paint(Graphics graphics) {
//Draw the background image and then call paint.
graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight){
super.sublayout(240,240);
setExtent(240,240);
}
};

Resources