This article applies to the following:
- BlackBerry® smartphones based on Java® technology
This article includes the following topics:
- Using backgrounds
- Identifying the types of backgrounds
- Using BackgroundFactory
To embellish your application, you may want to add a background image to a screen. Use the BlackBerry application programming interfaces (APIs) and the following few lines of code.
Note: The background image is applied to the first, or lowest level manager and is then added to a screen by overriding this manager's paint method.
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);
}
};
For a sample workspace that applies a background image to a screen, download the backgroundImage.zip file.
With BlackBerry® Device Software 4.6 and later, backgrounds can be added to any type of field, including managers and screens without having to override the paint method.
You can create any of the following types of backgrounds:
- Solid color
- Transparent
- Linear gradient
- Bitmap
To use background images, you must create a Background object using net.rim.device.api.ui.decor.BackgroundFactory as in the following code:
Background bg = BackgroundFactory.createSolidBackground(int colour);
Background bg = BackgroundFactory.createSolidTransparentBackground(int colour, int alpha);
Background bg = BackgroundFactory.createLinearGradientBackground(int color top left, int color top right, int color bottom right, int bottom left);
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap);
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap, int positionX, int positionY, int repeat);
where
colouris the value 0x00RRGGBB (red, green, blue)alphais the transparency level (from 0: clear to 255: opaque)Bitmapis an image usually created from the file as in the following example:
Bitmap bm = Bitmap.getBitmapResource("sample.png");positionXis the horizontal bitmap anchoring using any of the following positions:
POSITION_X_LEFTPOSITION_X_CENTERPOSITION_X_RIGHTpositionYis the vertical bitmap anchoring using any of the following positions:
POSITION_Y_LEFTPOSITION_Y_CENTERPOSITION_Y_RIGHTrepeatis the tiling option that can be any following values:
REPEAT_NONEREPEAT_HORIZONTALREPEAT_VERTICALREPEAT_BOTHREPEAT_SCALE_TO_FIT
To assign background to the specific field, manager or screen, the following is used:
field.setBackground(Background bg);field.setBackground(int visual, Background bg);field.setVisualState(int visual);
where
bgis the background created usingBackgroundFactoryor retrieved using the following:
field.getBackground();field.getBackground(int visual);visualis the visual state of the field, such as the following:
VISUAL_STATE_NORMALVISUAL_STATE_ACTIVEVISUAL_STATE_FOCUSVISUAL_STATE_DISABLEDVISUAL_STATE_DISABLED_FOCUS
If you want to create a transparent screen, you must set the transparent background to both the screen and its manager.