Thursday, July 23, 2009

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.


2 comments:

  1. You're one smart dude, thank you! This is very elegant.

    I'm learning BlackBerry Java, after many failed WebWorks attempts (not that I was POOR at it, just that the platform support is inconsistent... and Torch 9800 loves to crash randomly!). And, lets not get started on bbui.js!!

    So, got to learn java. I'm a beginner at it. I'm surprised how much of it is CUSTOM code. I'm sure, code like yours!

    Again, thank you!

    ReplyDelete