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
Regular Contributor
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Aligning buttons

I want to align 2 buttons horizontally.But both the buttons should be in center and not get pushed along the right or left edges of the screen.

 

I have come across many posts as to horizontal alignment.But what i want is them to be somewhat in center and not to extreme right or left.

 

This is my code for 2 buttons- SAVE and BACK

 

Please guide how to get this done.

 

ButtonField showInputButton = new ButtonField("\n\n\n  Save   ",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
          showInputButton.setChangeListener(new FieldChangeListener() 
          {
            
                public void fieldChanged(Field field,int context) 
                {
                    
                      Dialog.alert("Data Saved!");
                      
                      try
                      {    
                        //Open or create the database
                        Database db = DatabaseFactory.openOrCreate("database1.db");    
                        //Insert Data from db
              //Insert statement here
                        statement1.prepare();
                        statement1.execute();       
                        statement1.close(); 
                        db.close();
                        
                        }
                        catch(DatabaseException dbe)
                        {
                            System.err.println(dbe.toString());
                        } 
                        TextField1.getEditField().setText(" ");
                        TextField2.setText(" ");
                        TextField3.setText(" ");
                }
            }
                         
                        );
                        
          add(showInputButton);
        
    
      //BACK BUTTON
          ButtonField _btnGoBack = new ButtonField("\nGo Back",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
          _btnGoBack.setChangeListener(new FieldChangeListener() 
          {
                public void fieldChanged(Field field,int context) 
                {
                      UiApplication.getUiApplication().popScreen();
                }
           });
          add(_btnGoBack);

 Please guide.Appreciate the help.

 

Thanks.

 

 

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

Re: Aligning buttons

My cheap and cheerful way to do this is to add the two buttons to a HFM, with the style FIELD_HCENTER, then add this to a VFM.  The HFM will ask the buttons what width they want, and the size itself to match.  Then the VFM will center this.  Seems to work for me.

Highlighted
Developer
Posts: 40
Registered: ‎09-08-2011
My Device: 8900
My Carrier: AIS

Re: Aligning buttons

"somewhat in center" - just write a custom class - here's a big hint, but not the full story:

 

class HorizontalButtonManager extends HorizontalFieldManager
{
	int intManagerWidth = 0;

	public int getPreferredWidth()
	{
		// From http://supportforums.blackberry.com/t5/Java-Development/How-To-make-the-HorizontalFieldManager-Width...
		//return getScreen().getWidth();

		return intManagerWidth;
	}

	protected void sublayout(int width, int height)
	{
		// http://stackoverflow.com/questions/4183026/blackberry-horizontalfieldmanager-alignment

		// See HorizontalButtonFieldSet at http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-manage...

		int intFields = getFieldCount();
		if (intFields < 1)
		{
			intManagerWidth = 0;
			super.sublayout(width, height);
			return;
		}

		intManagerWidth = width;
		Field f;
		int intFieldsWidth = 0;
		int intField;
		// Do layout
		for (intField = 0; intField < intFields; intField++)
		{
			f = getField(intField);
			layoutChild(f, Math.max(0, width - intFieldsWidth), height);
			intFieldsWidth += f.getWidth();
		}

		if (intFieldsWidth > width)
		{
			// Let BB do it
			super.sublayout(width, height);
			return;
		}

		// Determine horizontal gap between buttons
		int intSpacing = 0;
		// Will only have spacing if there are 2 or more fields
		if (intFields > 1)
			intSpacing = Math.min(width / 12, intSpacing);

		// Determine initial gap, to centre the button sequence
		int intWidthUpto = (width - (intFieldsWidth + (intSpacing * (intFields - 1)))) / 2;

		// Do spacing
		for (intField = 0; intField < intFields; intField++)
		{
			f = getField(intField);
			setPositionChild(f, intWidthUpto, 0);
			intWidthUpto += f.getWidth() + intSpacing;
		}

		setExtent(intManagerWidth, intStandardButtonHeight);
	}
}