Blackberry background image? - blackberry

I have searched but haven't find out any solution, i have but its creating problems. when I insert this it says illegal argument and doesn't start.
Bitmap bitmap = Bitmap.getBitmapResource("Background.png");
this.getMainManager().setBackground(
BackgroundFactory.createBitmapBackground(bitmap)
);
It doesn't work.
Thanks for helping! Its working. Just a typo error of my path

tyy this -
final Bitmap top = Bitmap.getBitmapResource("your background image.png");
final VerticalFieldManager top_ = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, top.getWidth(),
top.getHeight(), top, 0, 0);
super.paint(graphics);
}
};
Now you add all your fields to this top_. then add the top_ to your screen. If there is no fields, it will not display the background image. so Dont forget to add some fields.

Related

Reducing Blackberry OS 5.0 DateField Height

Trying to reduce the height of a DateField from default 53 to 32, but to no avail. The height stays the same with the result that the text appears cut out towards the bottom of the background which is smaller in height (32).
I tried 2 ways:
DateField dtf = new DateField(label, defaultDateTime, DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY )
{
protected void layout(int width, int height)
{
Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, true);
width = bmp.getWidth();
height = bmp.getHeight();
super.setExtent(width, height);
super.layout(width, height);
}
};
2nd method (based on a Stack Overflow post):
public class MyDateManager extends VerticalFieldManager {
protected DateField dateField_ = null;
public MyDateManager(String label, long defaultDateTime) {
super(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
dateField_ = new DateField(label, defaultDateTime, DateField.DATE_TIME |
Field.FIELD_HCENTER | DrawStyle.HCENTER | Field.FOCUSABLE | Field.READONLY);
this.add(dateField_);
}
protected void sublayout(int width, int height) {
Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, false);
width = bmp.getWidth();
height = bmp.getHeight();
layoutChild(dateField_, width, height);
dateField_.setBackground(BackgroundFactory.createBitmapBackground(bmp, 0, 0, Background.REPEAT_NONE));
int h = dateField_.getHeight();
setExtent(width, height);
}
}
Please suggest.
Thanks
Second method is a horrible approach (messing with VerticalFieldManager's height will get you a lot of problems).
Try the first method again, but this time call super.layout first:
DateField dtf = new DateField(label, defaultDateTime, DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY )
{
protected void layout(int width, int height) {
super.layout(width, height);
Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, true);
width = bmp.getWidth();
height = bmp.getHeight();
super.setExtent(width, height);
}
};
Notice that using this approach you might also get glitches, as you are blindly overriding a method without considering the rest of the class. To do this safely you would need to have access to the parent class source, and you don't.
I'd suggest to extend HorizontalFieldManager (override sublayout) to have a fixed height (Never VFM because the scrolling goes in the same direction you are trying to make fixed). Then place inside a regular date field that uses all the available height (pass the flag Field.USE_ALL_HEIGHT in the constructor).
More info:
http://docs.blackberry.com/es-es/developers/deliverables/29251/Determining_the_dimensions_of_a_field_1578864_11.jsp
I suspect this is a battle you are likely to loose.
Personally I would create a DateField look-a-like from a LabelField which will give you the restrictions that you need.
DateField is a specialised Field and in my testing, it will ignore the limitations you put upon it, regardless of whether you try to do this via the containing Manager or directly in the Field's layout method. It seems the actual height it will choose for itself will vary by device.
I suspect on all touchscreen capable devices, it will try to give itself enough height to be touched. In terms of pixels that will depend on the resolution (dpi) of the device, as in fact your image should, because it will look different on a high resolution device, like the 9900 to a low resolution device, like the 9300.
I suspect on non-touchscreen devices, it will probably follow the font size specified.
I tested the following code:
DateField dtf1 = new DateField("test1", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY ) {
protected void layout(int width, int height) {
super.layout(width, 32);
System.out.println("height1: " + Integer.toString(this.getHeight()));
}
};
DateField dtf2 = new DateField("test2", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE ) {
protected void layout(int width, int height) {
super.layout(width, 32);
System.out.println("height2: " + Integer.toString(this.getHeight()));
}
};
DateField dtf3 = new DateField("test3", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE ) {
protected void layout(int width, int height) {
super.layout(width, 32);
System.out.println("height3: " + Integer.toString(this.getHeight()));
}
};
...
add(dtf1);
add(dtf2);
Font smallFont = this.getFont().derive(Font.PLAIN, 15);
dtf3.setFont(smallFont);
add(dtf3);
On a 9800, the DateField was always 40 pixels in height. On a 9300 is was 18 for the first 2 and 15 for the third.
So, since you are using this DateField just to display a non updateable value, I recommend instead you look at using something like SimpleDateFormat, to format the date as you want, and display this in a LabelField. You can then manage the height of the Field using the Font you specify.

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

What is the easiest way to set background image of the application on Blackberry

I know, this question was asked before. However I can't beleive that this operation is so difficult. I think i miss some important points on developing Blackberry applications.
Try to use the following code:
Bitmap backgroundBitmap = Bitmap.getBitmapResource("background.png");
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 super.paint
//to paint the rest of the screen.
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(),
backgroundBitmap, 0, 0);
super.paint(graphics);
}
};

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