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: 46
Registered: ‎01-14-2011
My Device: Talladega
My Carrier: Telus
Accepted Solution

Lay a UI object on top of another UI object

Hello friends and family,

 

So I'm looking to place a label field ontop of a bitmap image. Any idea how to do this? I want to bitmap image to be in the background and the label in the foreground (on top of it).

 

Thanks for the help.

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

Re: Lay a UI object on top of another UI object

Extend LabelField and override its paint / paintBackground to include painting of the bitmap you want.

If you would like to have a bigger field override your field's layout.

 

Write here if you need more specific examples and explanation.

----------------------------------------------------------
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: 46
Registered: ‎01-14-2011
My Device: Talladega
My Carrier: Telus

Re: Lay a UI object on top of another UI object

Yes please an example would be very helpfull arkaydz

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

Re: Lay a UI object on top of another UI object

OK, I'll make it more detailed.

 

1) Overriding paint:

  you need to make the field paint naturally, but you want to draw your bitmap before that. All this means a paint override similar to this:

 

protected void paint(Graphics g) {
  g.drawBitmap(0, 0, getWidth(), getHeight(), yourBitmap, 0, 0); // paints the image
  super.paint(g); // uses the built-in LabelField's paint to draw the text
}

 

 

2) Overriding layout:

  A Field's layout is called with two parameters - maximum width and maximum height this field can occupy. Its purpose it two-fold:

  a) to let the Field calculate its elements positions which it then uses in its paint to draw itself on the screen;

  b) to set the Field's actual size (called extent) - it is used by the Field's manager to calculate the positions and sizes of the rest of its Fields as well as to set the size of the Graphics object passed to the Field's paint.

You achieve a) by calling super.layout(width, height). It will set the Field's extent as well. However, you can then override this setting by explicitly using setExtent.

 

So your layout override might look like this:

 

protected void layout(int width, int height) {
  super.layout(width, height);
  setExtent(Math.min(yourBitmap.getWidth(), width), Math.min(yourBitmap.getHeight(), height));

 

I hope this was not too confusing.

 

----------------------------------------------------------
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: 46
Registered: ‎01-14-2011
My Device: Talladega
My Carrier: Telus

Re: Lay a UI object on top of another UI object

Thanks arkadyz. I'll give this a try tomorrow and get back to you. Thanks for all your help really appreciate your contributions to the community. 

Developer
Posts: 46
Registered: ‎01-14-2011
My Device: Talladega
My Carrier: Telus

Re: Lay a UI object on top of another UI object

Hey arkadyz,

 

I'm having problems using the protected void layout method I'm getting the error "cannont override the final method from Manager". The method name layout is underlined with red and tells me the above.

 

Developer
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: Lay a UI object on top of another UI object

You have to override layout a Label, etc.

 

If you subclass a Manager, override sublayout method instead.

Highlighted
Contributor
Posts: 33
Registered: ‎08-19-2010
My Device: Not Specified

Re: Lay a UI object on top of another UI object

I personnally prefer to create a custom manager.

 

here an exemple

 

 

public class CustomManager extends Manager {
    
    public CustomManager(long style){
        super(style);
    }

    protected void sublayout(int w, int h){
        Field field = null;
        int nb = getFieldCount();
        for(int i=0; i<nb; i++){
            field = getField(i);
	    layoutChild(field, w, h);
            int position[] = getPosition(i);
       	    setPositionChild(field, position[0], position[1]);
        }
        setExtent(w,h);
    }

    private int[] getPosition(int i){
	int position[] = new int[2];
	switch(i){
	case 0: //bitmap
		position[0] = 0;
		position[1] = 0;
		break;
	case 1://label
		position[0] = 10;
		position[1] = 10;
		break;
	}		
        return position;
    }

 

So in this code you create a manager, and for each field added to the manager, you indicate what are the coordinates (relative to the manager)

The easy way for you is to create the manager, and specify the height et width according to the bitmap size, place the bitmap at (0;0) and place the label where you want on the bitmap.

 

Naturally you have to add the bitmap to the manager before you add the label