Friday, 13 July 2012

How to set Background color of Blackberry Screen

For setting background color in Blackberry screen we need to do the following.

Disable Vertical scrolling of the main screen.

Now create a VerticalFieldManager with vertical scroll enabled. This will be the main manger where screen component have to add. Set the width and height of this manager as Screen width and height by overriding sublayout() method.
And override paint() method to set backgroung color

Here is the example code with above implemented.


import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

class TestScreen extends MainScreen{

    private VerticalFieldManager verticalManager;
    private HorizontalFieldManager hrzManager;
    
    TestScreen() 
    {    
        super(NO_VERTICAL_SCROLL);

        //rather than  adding component 
        //in the mainScreen add components
        //in this verticalManager and then
        //add this manager to mainScreen  
        verticalManager = new VerticalFieldManager(
                          Manager.VERTICAL_SCROLL | 
                          Manager.VERTICAL_SCROLLBAR)
        {
            public void paint(Graphics graphics)
            {
               //blue 
               graphics.setBackgroundColor(0x000000ff);
               graphics.clear();
               super.paint(graphics);
            }            
            protected void sublayout( int maxWidth, int maxHeight )
            {
               int width = Display.getWidth();
               int height = Display.getHeight();
               super.sublayout( width, height);
               setExtent( width, height);
            }
        };
     
        ButtonField button1 = new ButtonField("Button1");
        ButtonField button2 = new ButtonField("Button2");
        ButtonField button3 = new ButtonField("Button3");
        ButtonField button4 = new ButtonField("Button4");
        ButtonField button5 = new ButtonField("Button5");
        
        verticalManager.add(button1);
        verticalManager.add(button2);
        verticalManager.add(button3);
        verticalManager.add(button4);
        verticalManager.add(button5);
        this.add(verticalManager);
        
    }
}


Also have a look at this KnowledgeBase Article for getting better idea.

No comments:

Post a Comment