how to set background for blackberry - blackberry

i have used the following to set a background for blackberry ..no errors but in the output
when i scroll down the image gets repeated .. how to solve this problem .please help me
public final class MyScreen extends MainScreen
{
Bitmap background;
public MyScreen()
{
background = Bitmap.getBitmapResource("CVMS.jpg");
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH |
VerticalFieldManager.NO_VERTICAL_SCROLL);
vfm.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("CVMS.jpg"),Background.POSITION_X_LEFT,Background.POSITION_Y_TOP,Background.REPEAT_INHERIT));
add(vfm);
}
}
this is my complete code

All you need to do is change a flag in the following line
vfm.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("CVMS.jpg"),Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT));
You just need to use REPEAT_SCALE_TO_FIT instead of REPEAT_INHERIT

Have you tried using Background.REPEAT_NONE ?

Related

Adding background image to blackberry app not working

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.

Blackberry VerticalfieldManager virtual scroll view size too large

Developing on the blackberry (OS 7.0) and I have an extended Vertical Field manager created as such:
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL){};
However, when I scroll the view, the virtual scroll view size appears way too big.
i.e, I can scroll quite alot further down than is needed and I cant work out why?
Any body any quick ideas? I do have a background image in there that is created as such:
public void paint(Graphics graphics)
{
Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg.png");
Bitmap newBackground = new Bitmap(Display.getWidth(), Display.getHeight());
backgroundBitmap.scaleInto(newBackground, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
graphics.clear();
graphics.drawBitmap(0, 0, Display.getWidth(),
Display.getHeight(), newBackground, 0, 0);
super.paint(graphics);
}
Please and thanks,
Burrows
You can redefine the method to define the height of the manager
protected void setExtent(int width, int height) {
super.setExtent(width, myHeight);
}
To not repeat the background image is necessary to redefine the following method, returning false
protected boolean isScrollCopyable() {
return false;
}
Another comment is that it is a bad practice to obtain an image from the paint method. What you are doing is every time you call the paint method is going to get this image.
It's best to get the image once and then use it.
public MyScreen() {
super(NO_VERTICAL_SCROLL);
I managed to fix the issue in my extension of MainScreen using 'NO_VERTICAL_SCROLL' style parameter
Combined with Rupak's suggestion of setting the Background in the constructor of my Vertical Field Manager rather than overriding paint (
https://gist.github.com/3248319)
everything seems Good now - thanks all for your help.
Burrows
No need to USE_ALL_HEIGHT in your constructor.
// instead use this
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL){};

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

Resources