Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 20
Registered: ‎06-22-2010
My Device: Bold
My Carrier: Telcel
Accepted Solution

setBackground not working

I am making a Java App with Blackberry JDE 5.0 on Eclipse plugin.

 

I am trying to set a background on a MainScreen and  and I use:

 

setBackground(BackgroundFactory.createBitmapBackgr ound(Bitmap.getBitmapResource("bg.png")));

 

The image is currently in the res folder and I have no problem adding it as a BitmapField, but it doesn't show if I set it as background.

 

Here is the complete class code:

 

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class SettingsScreen extends MainScreen{
    
    public SettingsScreen(){
        super();
        setBackground(BackgroundFactory.createBitmapBackgr ound(Bitmap.getBitmapResource("bg.png")));
        setTitle("Propiedades");
       
        LabelField tf= new LabelField("");
        add(tf);
        tf.setText("test");
    }

}

 

 

Hope you can help me.

 

Thanks

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: setBackground not working

Unfortunately, setBackground does not work for Screens. You need to do something a bit more elaborate, including overriding paintBackground(), disabling scrolling, and putting the screen contents in a VerticalFieldManager.

 

See this article and also this thread for a discussion and sample code.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 282
Registered: ‎02-25-2010
My Device: curve,bold,storm
My Carrier: Verizon,AT&T

Re: setBackground not working

hi,

 

This method sometimes do not work even in my case

 

so u can try another way : Try to overridethe paint method of the manager and palce ur image there as background

 

Like:

 

VerticalFieldManager vfm = new VerticalFieldManager()
{
final Bitmap bg = Bitmap.getBitmapResource("back.png");
  protected void paint(Graphics g)
   {
     g.drawBitmap(0,0,bg.getWidth(),bg.getHeight(),bg,0,0);
   }
};

vfm.add(any field of ur choice);
add(vfm);


Now add all ur components to this manager. You can also use a custom manager or HorizontalFieldManager

 

 

Developer
Posts: 20
Registered: ‎06-22-2010
My Device: Bold
My Carrier: Telcel

Re: setBackground not working

With the blog post I could change the BG color but I want to put an image. I browse throw the forum post you added and I could get it working.

 

Any other idea??

 

Here is my code:

 

 

package ui;


import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class SettingsScreen extends MainScreen{
	
	private VerticalFieldManager verticalManager;
    private HorizontalFieldManager hrzManager;
    
    private Bitmap bg= Bitmap.getBitmapResource("splash_image_"+Main.getDimentions()+".png");
	
	public SettingsScreen(){
		super(NO_VERTICAL_SCROLL);
		verticalManager = new VerticalFieldManager(
                Manager.VERTICAL_SCROLL | 
                Manager.VERTICAL_SCROLLBAR)
{
  public void paint(Graphics graphics)
  {
	  int y = verticalManager.getVerticalScroll();

	  graphics.drawBitmap(0, y, bg.getWidth(), bg.getHeight(), bg, 0, 0);
     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);
  }
};
		this.setTitle("Propiedades");
		
		
		
		
		LabelField tf= new LabelField("",LabelField.FIELD_VCENTER);
		verticalManager.add(tf);
		tf.setText(DeviceInfo.getDeviceName());
		
		this.add(verticalManager);
	}

}

 

 

Highlighted
Developer
Posts: 20
Registered: ‎06-22-2010
My Device: Bold
My Carrier: Telcel

Re: setBackground not working

Hi, I use your code and it only paints a somall part of the image Like 5x5 px even though my image is 480x320 px.

 

Also I add a LabelField with some text and it doesn't show, here is the code:

 

 

public SettingsScreen(){
		super();
		VerticalFieldManager vfm = new VerticalFieldManager()
		{
		final Bitmap bg = Bitmap.getBitmapResource("splash_image_"+Main.getDimentions()+".png");
		  protected void paint(Graphics g)
		   {
		     g.drawBitmap(0,0,bg.getWidth(),bg.getHeight(),bg,0,0);
		   }
		};
		
		LabelField tf= new LabelField("ferrrr");
		vfm.add(tf);

		add(vfm);
		setTitle("Propiedades");
	}

 

 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: setBackground not working

Try something like this (untested):

 

public class SettingsScreen {
final Bitmap bg = Bitmap.getBitmapResource("splash_image_"+Main.getDimentions()+".png");
public SettingsScreen() {
super(NO_VERTICAL_SCROLL);
VerticalFieldManager vfm = new VerticalFieldManager(
USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
LabelField tf= new LabelField("ferrrr");
vfm.add(tf);
add(vfm);
setTitle("Propiedades");
}
protected void paintBackground(Graphics g) {
super.paintBackground(g);
g.drawBitmap(0,0,bg.getWidth(),bg.getHeight(),bg,0,0);
}
}

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 20
Registered: ‎06-22-2010
My Device: Bold
My Carrier: Telcel

Re: setBackground not working

Not getting any luck, If you have any more suggestions I will appreciate it.

 

Thanks for your posted solutions.

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: setBackground not working

Just as a weird idea:

 

getDelegate().setBackground(...);

 Will that work?

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 20
Registered: ‎06-22-2010
My Device: Bold
My Carrier: Telcel

Re: setBackground not working

This code worked:

 

 

package ui;



import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;


public class SplashScreen extends MainScreen {
	
	Bitmap bg=Bitmap.getBitmapResource("splash_image_"+Main.getDimentions()+".png");
	
	VerticalFieldManager verticalManager= new VerticalFieldManager(USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
	
	
	public SplashScreen(){
		super();
		//SETS background image
		verticalManager.setBackground(BackgroundFactory.createBitmapBackground(bg));
		
		
		//ADDS Vertical manager REQUIRED!!!!
		add(verticalManager);
	}
	
}

 

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: setBackground not working

When the screen scrolls, does your background scroll along with it?  I have a feeling this will be the case...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!