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: 445
Registered: ‎07-15-2008
My Device: Not Specified
Accepted Solution

Buttons and Horizontal Manager

Hello,

I would want to allign two buttons new to each other using the HorizontalFieldManager.

The one button should be on the most left side of the screen, the other one on the most right side of the screen.

 

I tried this code but both buttons are on the left, attached to each other:

 

 

HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL); hfm.add(new ButtonField("Left Button",ButtonField.FIELD_LEFT)); hfm.add(new ButtonField("Right Button",ButtonField.FIELD_RIGHT)); add(hfm);

 

 

 

 

The "Left Button" has the style: ButtonField.FIELD_LEFT so it should be on the left;

The "Right Button" as the ButtonField.FIELD_LEFT style and should be on the right - but it isn't.

 

Can someone help me on that? 

 

Thank you. 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Buttons and Horizontal Manager

First make sure that the HFM is using the full width available... otherwise it will just use as much width as it needs to show the two buttons.

 

If it is using the full width, but doesn't align them like you want, you can align them manually:

 

Override the HorizontalFieldManager class.

Send style to the superclass: USE_ALL_WIDTH | NO_HORIZONTAL_SCROLL

Override the getPreferredWidth().

Override the sublayout().

 

 

public int getPreferredWidth() { return getManager().getWidth(); }

 

 

 

protected void sublayout(int maxWidth, int maxHeight) { super.sublayout(maxWidth, maxHeight); int width = getWidth(); //Field leftButton = getLeftButton(); //if (leftButton != null && equals(leftButton.getManager()) { // // int x = 0; // int y = 0; // setPositionChild(leftButton, x, y); //} Field rightButton = getRightButton(); if (rightButton != null && equals(rightButton.getManager()) { int x = width - rightButton.getWidth(); int y = 0; setPositionChild(rightButton, x, y); } }

 

 

Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Re: Buttons and Horizontal Manager

Martin thank you for your effort.

 

What does this line do:

 

Field rightButton = getRightButton();

 

 I don't exactly know how to override the HFM.

 

Do I replace this code I had: 

 

HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH | NO_HORIZONTAL_SCROLL); hfm.add(new ButtonField("Left Button",ButtonField.FIELD_LEFT)); hfm.add(new ButtonField("Right Button",ButtonField.FIELD_RIGHT)); add(hfm);

 Or do I leave it?

 

Thank you. 

 

 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
BlackBerry Development Advisor
Posts: 15,055
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Buttons and Horizontal Manager

The code you have pasted results in ButtonFields on the same line when the HorizontalFieldManager is added to a MainScreen.  Where are you adding the hfm to?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Re: Buttons and Horizontal Manager

I try to add the hfm to the mainscreen and that works.

 

But I want the one button on the most right side and the other button on the most left side. 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
BlackBerry Development Advisor
Posts: 15,055
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Buttons and Horizontal Manager

Add 2 VerticalFieldManagers to your HorizontalFieldManager and add a ButtonField to each one.  Use the default style for the first/left VerticalFieldManager and the Manager.USE_ALL_WIDTH style for the second/right VerticalFieldManager.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Re: Buttons and Horizontal Manager

Like this:

 

ButtonField bLeft = new ButtonField("left"); ButtonField bRight = new ButtonField("right"); HorizontalFieldManager hfm = new HorizontalFieldManager(); VerticalFieldManager vfm1 = new VerticalFieldManager(); VerticalFieldManager vfm2 = new VerticalFieldManager(Manager.USE_ALL_WIDTH); vfm1.add(bLeft); vfm2.add(bRight); hfm.add(vfm1); hfm.add(vfm2); add(hfm);

 

 

But that results in the two buttons just right next to each other.

I think I have a dumb moment. ;-) 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Buttons and Horizontal Manager

Three managers just to position two buttons seems a bit excessive.

 

 

private class MyHorizontalFieldManager extends HorizontalFieldManager { private ButtonField leftButton; private ButtonField rightButton; public MyHorizontalFieldManager() { super( USE_ALL_WIDTH ); ButtonField leftButton = new ButtonField( "Left" ); setLeftButton( leftButton ); add( leftButton ); ButtonField rightButton = new ButtonField( "Right" ); setRightButton( rightButton ); add( rightButton ); } protected void sublayout( int maxWidth, int maxHeight ) { super.sublayout( maxWidth, maxHeight ); int width = getWidth(); // ButtonField leftButton = getLeftButton(); // if (leftButton != null && equals( leftButton.getManager() )) { // // int x = 0; // int y = 0; // setPositionChild( leftButton, x, y ); // } ButtonField rightButton = getRightButton(); if (rightButton != null && equals( rightButton.getManager() )) { int x = width - rightButton.getWidth(); int y = 0; setPositionChild( rightButton, x, y ); } } private ButtonField getLeftButton() { return leftButton; } private void setLeftButton( ButtonField leftButton ) { this.leftButton = leftButton; } private ButtonField getRightButton() { return rightButton; } private void setRightButton( ButtonField rightButton ) { this.rightButton = rightButton; } }

 

 

 

New Developer
Posts: 7
Registered: ‎09-24-2008
My Device: Not Specified

Re: Buttons and Horizontal Manager

hi all,

  thanks for the explanation. I am using the customButtons, by using that i am unable to place the buttons at the left and right corners.The buttons were placing next to each other. ca

  can anybody tell me how to make them apart. if the same code i am using for ButtonField it is working fine by placing the two buttons on two corners.

  Is there anything i need to change in the cutomButton class?

 

Thanks

lakshman

Developer
Posts: 39
Registered: ‎04-09-2009
My Device: Not Specified

Re: Buttons and Horizontal Manager

javec, your code following the suggestion of MSohm:

 

 

ButtonField bLeft = new ButtonField("left");
ButtonField bRight = new ButtonField("right");

     HorizontalFieldManager hfm = new HorizontalFieldManager();
     VerticalFieldManager vfm1 = new VerticalFieldManager();
     VerticalFieldManager vfm2 = new VerticalFieldManager(Manager.USE_ALL_WIDTH); 
     vfm1.add(bLeft);
     vfm2.add(bRight);
     
     hfm.add(vfm1);
     hfm.add(vfm2);
        
     add(hfm);

 

 

works fine if you just add ButtonField.FIELD_LEFT to the first button, and ButtonField.FIELD_RIGHT to the second.

 

Possibly the first FIELD_LEFT is unnecessary.  I think this is simpler than the later solution in the thread.  I don't understand the semantics of FIELD_LEFT, FIELD_RIGHT for buttons -- why they behave differently for horizontal vs vertical managers.