Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Use a background image in application screens

by Retired on ‎02-16-2010 03:07 PM - edited on ‎11-08-2010 04:29 PM by Retired (23,806 Views)



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 |

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



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




  • colour is the value 0x00RRGGBB (red, green, blue)
  • alpha is the transparency level (from 0: clear to 255: opaque)
  • Bitmap is an image usually created from the file as in the following example:

    Bitmap bm = Bitmap.getBitmapResource("sample.png");

  • positionX is the horizontal bitmap anchoring using any of the following positions:
  • positionY is the vertical bitmap anchoring using any of the following positions:
  • repeat is the tiling option that can be any following values:

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


  • bg is the background created using BackgroundFactory or retrieved using the following:
    • field.getBackground();
    • field.getBackground(int visual);
  • visual is the visual state of the field, such as the following:

Additional Information


If you want to create a transparent screen, you must set the transparent background to both the screen and its manager.

by Developer
on ‎09-29-2010 04:36 PM

In pre-4.6 code, painting the background is OK as long as people use non-scrolling managers.  If, for some reason, a developer adds HORIZONTAL_SCROLL and/or VERTICAL_SCROLL to the style bits and the manager scrolls, the background will scroll along with it.


A more robust solution would be

//Draw the background image and then call super.paint
//to paint the rest of the screen.
graphics.drawBitmap(getHorizontalScroll(), getVerticalScroll(), graphics.getScreenWidth(), graphics.getScreenHeight(), backgroundBitmap, 0, 0);

This way the background image will stay "docked".

by Developer
on ‎10-05-2010 08:39 AM

graphics.getScreenWidth and getSreenHeight are deprecated. Use Display.getWidth() and Display.getHeight() instead.

by Developer
on ‎10-14-2010 05:06 PM

Yes, that's true.  I've just left those alone since they do not illustrate my point of non-scrolling background.


In fact, the best values for width and height would be either

    getVisibleWidth(), getVisibleHeight()


    backgroundBitmap.getWidth(), backgroundBitmap.getHeight()

Users Online
Currently online: 14 members 778 guests
Please welcome our newest community members: