Question

i am new to blackberry development and want to add an image to a sample blackberry application. I have tried multiple tutorials, but the image is not displaying in the background

can any one kindly tell me what is the problem?

/**
 * This class extends the UiApplication class, providing a graphical user
 * interface.
 */
public class Diverse extends UiApplication {

    private Bitmap backgroundBitmap;
    private Bitmap fieldBitmap;


    public static void main(String[] args) {

        Diverse diverse = new Diverse();
        diverse.enterEventDispatcher();
    }

    /**
     * Creates a new MyApp object
     */
    public Diverse() {
        //The background image.
        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);
            }            

        };

        //The LabelField will show up through the transparent image.
        LabelField labelField = new LabelField("This is a label");

        //A bitmap field with a transparent image.
        //The background image will show up through the transparent BitMapField image.
        BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png"));

        //Add the manager to the screen.
        mainScreen.add(horizontalFieldManager);

        //Add the fields to the manager.
        horizontalFieldManager.add(labelField);
        horizontalFieldManager.add(bitmapField);

        // Push a screen onto the UI stack for rendering.
        pushScreen(new DiverseScreen());
    }
}

and DiverseScreen class is

package diverse;

import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class DiverseScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public DiverseScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Diverse");
    }
}
Was it helpful?

Solution

The problem is that you have set the background image for one screen, but you never displayed that screen. Then, you displayed a different screen that did not have a background image set.

First, it helps to understand the BlackBerry UI framework. It allows you to create a hierarchy of objects on screen. At the top level, you have a Screen (or subclass of Screen), and then inside of the Screen, you have Managers, and inside them are Fields. But, each level must be added to a container of some kind, and finally, the top-level Screen must be displayed with something like pushScreen().

See a little more description on this hierarchy in a recent answer here

In your situation, you should change this line

    MainScreen mainScreen = new MainScreen();

to

    DiverseScreen mainScreen = new DiverseScreen();

and then change this line

    pushScreen(new DiverseScreen());

to

    pushScreen(mainScreen);

since mainScreen is the instance where you added a horizontal field manager that draws your background image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top