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
Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone
Accepted Solution

Centering LabelFields in PopUp Screen

I'm trying to center a PopUp Screen and add some LabelFields to it which should be centered too. I have set the position of the Popup manually, but the LabelFields are still not aligning properly.. What have I done wrong?

 

Positioning of the PopupScreen which is more or less centered now:

 

private final static int _X = (Display.getWidth() - 510);
private final static int _Y = (Display.getHeight() - _Height) >> 1;

public void sublayout(int width, int height) {
		super.sublayout(getPreferredWidth(), Integer.MAX_VALUE);
		setExtent(getPreferredWidth(), getPreferredHeight());
		setPosition(_X, _Y);
	}

 

These are the LabelFields on it which are still more on the left side:

 

LabelField title = new LabelField("UI Test App",
				Field.FIELD_HCENTER);
LabelField version = new LabelField("Version X.Y",
				LabelField.FIELD_HCENTER);

add(title);
add(version);

 

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Centering LabelFields in PopUp Screen

[ Edited ]

Can I first say I don't like your sublayout method which is attempting to centre the Popup Screen.  Two reasons:

1) I thought Popup screens are centered by default, so I am not sure you need to this code. 

2) Your code is device specific, and I think if you had thought about this some more, you could have created a generic method.  To investigate how you could make this more generic, I think you need to understand more what Managers actually do and how to write one.  So I would recommend you review the following:

http://supportforums.blackberry.com/t5/Java-Development/How-to-Extend-Manager/ta-p/446749

http://supportforums.blackberry.com/t5/Java-Development/Create-a-custom-layout-manager-for-a-screen/...

 

Creating your own Manager would also be one way you could get your LabelFields centered.  However if you define them as follows, I think the text will be centered for you, which may achieve what you require.

 

LabelField title = new LabelField("UI Test App",
Field.USE_ALLWIDTH | DrawStyle.HCENTER);

 

let us know how you get on with this and why you think you need to center the popup screen yourself.

Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Centering LabelFields in PopUp Screen

[ Edited ]

You're right, usually the Popup is centerede automatically. I have implemented the sublayout method mainly to change the width and height of the Popup Screen. If I just use setExtent in the sublayout method the Popup never shows up so I had to set the position additionally.

 

I already tried the code example you suggested but the effect stays the same.

 

Is there any possibility to change the width of the Popup without having to set the position?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Centering LabelFields in PopUp Screen

If you want to change the width and height of the PopupScreen, then I suspect that all you should have to do is override sublayout and call super.sublayout with your required height and width.  This will set the bounds that the manager can use - what it does use is up to it.  If you ever call super.sublayout you should not need to do any setExtent processing yourself - if you review the KB articles I pointed you at I suspect you will understand why not. 

 

"I already tried the code example you suggested but the effect stays the same."

 

I presume here you are talking about the LabelField sample I gave you.  Can you give us a complete sample, using standard RIM supplied LabelField and PopupScreen, to demonstrate the problems you face.  Make sure this is as small a sample as possible and uses only standard Fields so that we can try it ourselves without needing anything else. 

Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Centering LabelFields in PopUp Screen

Okay, when I try to set the width and height with the super.sublayout method the Popup Screen is being cropped but positioned in the top left corner of the screen..

 

public void sublayout(int width, int height) {
		super.sublayout(getPreferredWidth(), getPreferredHeight());
	}

 Is that what you suggested?

Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Centering LabelFields in PopUp Screen

[ Edited ]

 

This is my sample in short:

 

public InfoScreen() {
		super(new VerticalFieldManager());

		LabelField title = new LabelField("UI Test App", Field.FIELD_HCENTER);
		LabelField version = new LabelField("Version X.Y",
				LabelField.FIELD_HCENTER);
		closeBtn = new ButtonField("Close", ButtonField.FIELD_HCENTER);
		closeBtn.setChangeListener(this);

		add(title);
		add(version);
		add(closeBtn);
	}

 

As soon as I add the sublayout method the position has to be set too. So far it also made no difference when I changed the LabelField attributes to DrawStyle.HCENTER, USE_ALL_WIDTH and so on..

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Centering LabelFields in PopUp Screen

I have just run that code, and I got a nicely centered popup - see attached

 

I suspect you want something different to just a centred popup screen and labels.  Perhaps you could explain what you are actually trying to achieve?

Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Centering LabelFields in PopUp Screen

[ Edited ]

Alright I see, it's been the separatorField I added to it too! I couldn't find anything about it but it seems the separatorField has a standard length in a popup if you don't customize it?

This is how it looked like before with the separatorField, that was why I wanted to reduce the width of the popup!

 

Popup_Centered-01.png

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Centering LabelFields in PopUp Screen

[ Edited ]

"it seems the separatorField has a standard length in a popup if you don't customize it?"

 

Sort of.  In fact the SeparatorField wants to put in a Separator that is as wide as the Manager it is in. so in effect, it will force the Manager to use all the width that it is given.

 

One way round this is to restrict the width that the Manager is given. Here is some code to demonstrate how to do that.  See screen shot for the result. 

 

	public InfoScreen() {
		super(new VerticalFieldManager() {
			public void sublayout(int width, int height) {
				super.sublayout(Math.min(width, Display.getWidth()/2), height);
			}
		});
		LabelField title = new LabelField("UI Test App", Field.FIELD_HCENTER);
		LabelField version = new LabelField("Version X.Y",
				LabelField.FIELD_HCENTER);
		ButtonField closeBtn = new ButtonField("Close", ButtonField.FIELD_HCENTER);
		add(title);
		add(version);
		add(new SeparatorField());
		add(closeBtn);
	}

 There are other options, for example, you could use the layout in the SeparatorField to restruct its width.  Then the Manager would not be forced to use all its width by the SeparatorField.  The best option will depend on exactly what you are trying to achieve and what other restictions you have. 

 

Highlighted
Contributor
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Centering LabelFields in PopUp Screen

[ Edited ]

Okay so I changed the width of the wrong element.. ;-)

Thank you so much, that is exactly what I was trying to achieve!