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
anditsung
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2
Accepted Solution

verticalfieldmanager will not center the child layout

 

private VerticalFieldManager vfm = new VerticalFieldManager( Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Manager.FIELD_HCENTER) {
		
		int width = Display.getWidth();
		int height = Display.getHeight() - 80;
		
		public void sublayout(int maxWidth, int maxHeight) {			
            super.sublayout( width, height);
            int bagi = (Display.getWidth() - this.getField(0).getWidth()) / 2;
            Field field = this.getField(0);
            setPositionChild(field, bagi, 0);
            setExtent( width, height);
		}
	};

 i got a problem displaying the child layout for verticalfieldmanager.

 

the child layout will not horizontal center on this manager when create a fixed size for the manager.

anyone know the solution?

setpositionchild will not fix the problem.

Please use plain text.
Developer
simon_hain
Posts: 16,216
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: verticalfieldmanager will not center the child layout

most likely this.getField(0) returns a field that was not handled by layout yet, that means it does not have a getWidth value. You can use getPreferredWidth to check what the field requests before layout.

If you overwrite sublayout don't call super.sublayout.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: verticalfieldmanager will not center the child layout

[ Edited ]

Try this code --

HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH)
{
protected void sublayout(int maxWidth,int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setPositionChild(this.getField(0), (maxWidth-labelField.getContentWidth())/2, 0 );
}
};

 

Use vertical field manager instead of horizontal field manager

Please use plain text.
Developer
anditsung
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2

Re: verticalfieldmanager will not center the child layout

[ Edited ]

@simon

my code working fine using os 5 and 6.
but on os7 and os71 not working at all.
the getfield(0).getwidth will always return the maxwidth of the screen. not returning the width of the field.


@developer

already test the code. 

same as my code on os 5 and 6

not working on os 7 and 71

maybe the os bug?

Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: verticalfieldmanager will not center the child layout

use object name of field instead of getfield(0) and try?
Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: verticalfieldmanager will not center the child layout

[ Edited ]

Looking at this code, I think you need to understand better what a Manager needs to do in sublayout and the constraints that it works under.

 

Let us look at your code:

 

  public void sublayout(int maxWidth, int maxHeight) {
            super.sublayout( width, height);
            int bagi = (Display.getWidth() - this.getField(0).getWidth()) / 2;
            Field field = this.getField(0);
            setPositionChild(field, bagi, 0);
            setExtent( width, height);
}

 

There are lots of points to note, please bear with me:

 

You specify your Manager with Manager.FIELD_HCENTER.  What this is doing is trying to tell the Manager you add this into (call this the father Manager), that this child Manager should be centered horizontally.  So the father will layout this child Manager, then determine how wide the child Manager is, then figure out how wide the father Manager is, and finally position the child Manager in the centre.  The assumption here is that this Manager will have some space at either side.  Whether it does actually have this, will depend on the type of Manager and the other Fields in it and how you are have specified.  But this setting by itself could solve your problem.  The fact that it doesn't suggests there is another issue elsewhere. 

 

Note that the space provided to the Manager is the maximum it can use.  Since you are trying to restrict the width and height, in you case, the area you have to play with is bounded by

1) the minimum of maxWidth and Display.getWidth() - 80, in width and

2) the minimum of maxHeight and Display.getHeight()

You need to check this before using a value for your processing.

 

You call super.layout(), then effectively ignore everything that it does, with the exception that it will layout all the Fields, so this value should be accurate:

this.getField(0).getWidth()

However what we don't know is how you have requested this Field be laid out.  You could for example, have told this FIeld to USE_ALL_WIDTH.  In that case the value for bagi would have been 0 and that means this Manager can not hope to 'center' the Field. 

 

I recommend that you review these KB articles for further information on how to program a Manager. 

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

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

 

In this particular case, you can actually center a Field in a VFM just using the FIELD_HCENTER style.  If this doesn't work, then there is most likely an issue with the Field, such as it being an EditField that always takes the whole width.  You will not solve this by using your own Manager. 

 

In addition, in this case, we can't actually resolve this problem for you.  You can debug this further and determine exactly why the Field is not being centered.  Is it because bagi is 0 for example?  Hopefully the knowledge you will gain from reviewing the articles again will help you debug this. 

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: verticalfieldmanager will not center the child layout

I see things have moved on since I started.

 

1) Don't use the object name, that won't help.

 

2) You should be able to do this without an additional Manager.

 

3) What sort of Field is this that you are trying to center?  It sounds like it has changed style between OS levels.  We need to understand this Field before we can further help resolving this problem. 

Please use plain text.
Developer
anditsung
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2

Re: verticalfieldmanager will not center the child layout

the child field for vfm was gridfieldmanager. when i try to get the width for gridfieldmanager it was the same as the width for vfm. i alwasy get 0 for bagi on os 7 and 71.

but i get the correct value for os 5 and 6.

about using the super.sublayout()
if im not using that the child for the manager will not be display at all.
int bagi = (Display.getWidth() - this.getField(0).getWidth()) / 2;
Field field = this.getField(0);
setPositionChild(field, bagi, 0);
setExtent( width, height);

maybe i could make a customa layout manager as you suggest. will inform later with the progress
Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: verticalfieldmanager will not center the child layout

[ Edited ]

You say this

 

"about using the super.sublayout()
if im not using that the child for the manager will not be display at all."

 

If you review how Managers work, you will see that they layout their children by calling the layoutChild() method.  If you remove super.layout and don't call layoutChild() yourself, then your GridFieldManager never gets laid out.  As a result, it is not displayed.  The fact that you do not appreciate this at this time supports my comment from earlier " I think you need to understand better what a Manager needs to do in sublayout"

 

You do not need to create your Manager here.  As I noted above, in fact you don't even need a special Manager for this.  No Manager can 'center' a Field that uses the full width available. 

 

Since there is no GridFieldManager supplied by RIM in OS 5.0, the 'Field' you are trying to center is running your own code.  The fact that in OS 6.0 and above it uses the full width rather than the actual width required would suggest that GridFieldManager is the code you should be looking at, not your centering Manager. 

 

So the next step in resolving this problem is to review your GridFieldManager code and see why it uses the full width.

 

Edit:

Apologies, there is a GFM in OS 5.0.  Incorrect comment left in to explain following Posts. 

Please use plain text.
Developer
anditsung
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2

Re: verticalfieldmanager will not center the child layout

actually there is gridfieldmanager for os5. the gridfieldmanager is supplied by rim since BlackBerry API 5.0.0.

yes. now i know how to use the super.sublayout from your comment above. i shoud call layoutchild to display the child field for my manager. thanks.

sorry about the bagi variable.. it wasnt 0.
but the gridfieldmanager.getwidth() will return 0. when i try to debug.

but i create a button to display width of the gridfieldmanager.
when i click a button the width is display correctly but when i get the width from sublayout will return 0.
Please use plain text.