Adding background image to blackberry app not working - blackberry

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

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.

Related

Loading Screen wheel with using Blackberry JAVA SDK 6.0 Activity Indicator UI element

In my project,I want to show a loading screen with a rolling wheel image(possibly with a .gif file) while my http connection occured.
My code is below. It extends a class which extends MainScreen. I show this screen when the user clicked login button.
public class MSWheelScreen extends MSScreen{
//Constructor
public MSWheelScreen(){
super();
add(new SeparatorField());
add(new LabelField("Loading...", Field.FIELD_HCENTER));
add(new SeparatorField());
add(new LabelField());
ActivityIndicatorView myview = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel mymodel = new ActivityIndicatorModel();
ActivityIndicatorController mycontroller = new ActivityIndicatorController();
myview.setController(mycontroller);
myview.setModel(mymodel);
mycontroller.setModel(mymodel);
mycontroller.setView(myview);
mymodel.setController(mycontroller);
Bitmap mybitmap = Bitmap.getBitmapResource("img/wheel.gif");
myview.createActivityImageField(mybitmap, 5, Field.FIELD_HCENTER);
add(myview);
}
}
Anyway; my problem is that, I cant show the wheel image as i wanted. I can only see the part of the wheel, i am not able to see the whole .gif file as i open it in a browser. So i want to adjust the .gif file that i have added on the loading screen. I want to know some built in methods that i can use with activity indicator UI elements to adjust my gif.
The link for my sample run screenshot:
http://imageshack.us/photo/my-images/191/9800j.jpg/
The link for original gif.
http://imageshack.us/photo/my-images/810/ajaxloaderw.gif/
It is difficult to implement loading screen with .gif bcz it required one more thread that handle .gif image
So i always implement Loading screen in blackberry like this :
This is simple code for loading screen ....
HorizontalFieldManager popHF = new HorizontalFieldManager();
popHF.add(new CustomLabelField("Pls wait..."));
final PopupScreen waitScreen = new PopupScreen(popHF);
new Thread()
{
public void run()
{
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().pushScreen(waitScreen);
}
//Here Some Network Call
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().popScreen(waitScreen);
}
}
}.start();

Create Transparent Mainscreen in Blackberry

i want to create a type of MainScreen in my Blackberry app which should be Transparen/Translucent.
I tried with following code on MyCustomMainScreen's constructor but it still shows a white screen
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
this.setBackground(bg);
I have read in the forum and also tested it for popup screens and it works fine with them but fails with mainscreens.
Does anyone have any idea how to achieve this for MainScreen..?
-Big O
override the paint method like:
class M extends MainScreen
{
public M()
{
setBackground(BackgroundFactory.createSolidTransparentBackground(Color.ANTIQUEWHITE, 100));
LabelField l=new LabelField("hello");
add(l);
}
protected void paint(Graphics graphics)
{
super.subpaint(graphics);
}
}
Instead of
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
this.setBackground(bg);
maybe better
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
getMainManager().setBackground(bg);
I found the solution without overriding the paint method. You need to set the background full transparent for the Screen and translucent for the main manager:
// Full transparent background
setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
// Manager translucent background
getMainManager().setBackground(BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50));

Customize field in blackberry

Can you please tell me how to customize my components at a BlackBerry?
E.g. I need a verticalfieldManager which will have a label field, an image, placed one after the other. Also this verticalFieldManager should be in the center of the screen, it should not start from the immediate left or right of the screen.
I want a border for this verticalFieldManager, too.
It should be noted that the BorderFactory class wasn't added until JDE 4.6.0. If you are your application on a older JDE platform then you will have to override the vertical manager's paint method to draw the border.
-Glen
No need to customized the class. Hope, following code will solve your problem.
public class Sample extends UiApplication {
public Sample() {
pushScreen(new SampleScreen());
}
public static void main(String[] args) {
Sample sample = new Sample();
sample.enterEventDispatcher();
}
private static class SampleScreen extends MainScreen {
public SampleScreen() {
VerticalFieldManager vfManager = new VerticalFieldManager(Field.FIELD_HCENTER);
vfManager.add(new LabelField("Test Label"));
vfManager.add(new BitmapField(Bitmap.getBitmapResource("image.png")));
vfManager.setBorder(BorderFactory.createRoundedBorder(
new XYEdges(5, 5, 5, 5),
Color.ORANGE,
Border.STYLE_SOLID
));
add(vfManager);
}
}
}
You can also create custom manager and set the position of contents as per your choice and requirement
now add your custom manager to a manager say basemanager and add this basemanager to screen

Image over a background Image in Blackberry

I want to set the position of one image over a background image. The position could be anywhere on the screen.
Can I have a sample code or a link or tutorial for that?
Here's how I do it:
This works in 4.6.0 and later because of BackgroundFactory
// Create the background image and the image field to put on top
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource(bgImgPath);
Bitmap bmp = Bitmap.getBitmapResource(imgPath);
BitmapField imgField = new BitmapField(bmp);
// Create the field manager
VerticalFieldManager manager = new VerticalFieldManager()
{
// Overide the sublayout of the field manager to set the position of
// the image directly
protected void sublayout(int width, int height)
{
setPositionChild(imgField, positionX, positionY)
setExtent(width, height)
}
};
// Set the background of the field manager
manager.setBackground(bg);
// add the bitmap field to the field manager
manager.add(imgField);
// add the field manager to the screen
add(manager);
For multiple images you can make a layout manager class and use that position all your images where you want them using similar techniques. There's a tutorial for making and using a layout manager, I'll try and dig it up and post it back here.
If your using 4.5.0 or earlier, I use a layout manager and just add the background image like any other image but add it first so it draws on the bottom.
Like I said I'll try and find that tutorial for the Layout Manager.
You can create a class that extends Manager class
Here you can specify the background image as well as you can position the other image at a position you want
class Test extends MainScreen
{
Test()
{
super();
Bitmap bmp = Bitmap.getBitmapResource("image1.png");
BitmapField bmpf = new BitmapField(bmp);
Mymanager obj = new Mymanager();
obj.add(bmpf);
}
}
class Mymanager extends Manager
{
final Bitmap background = Bitmap.getBitmapResource("back.png");
protected void paint(Graphics g)
{
g.drawrect(0,0,background.getWidth,background.getheight,background,0,0);
}
protected void sublayout(int width, int height)
{
Field field = getfield(0);
layoutchild(field,100,100);
setPositionchild(field,20,10);
setExtent(Display.getWidth,Display.getHeight);
}
}

please tell me how i can paint the whole screen in a rimlet?

i am developing a blackberry app. i want to paint the screen. i have implemented this
VerticalFieldManager hfmBg = new VerticalFieldManager(Field.USE_ALL_HEIGHT )
{
protected void paint(Graphics g)
{
g.setBackgroundColor(Color.SILVER);
g.clear();
super.paint(g);
}//end of paint method
};//end of vertical field manager
but tell me is there any other approach to paint the whole screen?
You have the right idea - just put that paint method in your Screen (MainScreen or FullScreen) class instead of your Manager class. You may also have to add the following empty method to your Screen class to override themes that set the default colors:
protected void applyTheme() {
}

Resources