Even distribution of views in a layout - android-layout

Is there any way, I can add UI components (Buttons in my case) to any layout (RelativLayout in my case) and width of parent gets evenly distributed among all views.
say parent width = 100;
if I add 10 Buttons - all buttons should be of width 10.
thanks.
m

If you use a LinearLayout you can make use of layout_weight to evenly distribute the size.
For eg, if you have two buttons, to take half width each of its parent, you can give "layout_weight=1" in both the buttons. So both of them would share the space.
Checkout the layout_weight documentation for more details

Related

Which layout would be the best one for my application?

I'm new to android. I want to ask which layout is best and easy to use in xml files.
I'm confused with constraint layout and linear or relative layout.
Relative Layout:
A relative layout displays its views relative to one another, so order is not that important. You can define the top most view at the end of the layout and provide details to show it on top left. The following attributes are used to define relative layouts:
Position relative to screen: You can align a view relative to screen using alignParentTop, centerHorizontal etc.
Position relative to other views: You can align a view relative to another view using above, below, toLeftOf etc.
Margins: You can provide margins using marginTop, marginLeft etc.
Linear Layout:
A linear layout displays its views next to each other either vertically or horizontally. So, if you define views in a row, they will be displayed one after the other. You need to specify orientation to define whether layout is vertical or horizontal. The following attributes are used to define linear layouts:
Weight: It specifies how much space each view spans relative to others. For example, in an e-mail application, you can give less weight to ‘To’ and ‘Subject’, and more weight to ‘Message’.
Gravity: It defines placement of a view’s contents. For example, if a view spans entire screen, but has only one line of text, then you can decide whether it should be displayed on top, center or bottom.
Layout Gravity: It defines the placement of the view itself.

Layout with many items autoresizing

I'm using Xamarin, writing for iOS.
I'm trying to make keyboard view with numbers,
it will be 4x4.
How can I make that 4 buttons in line will be same width depending on screen width?
Edit: Seems like it can be done that way: make layout with hardcoded height, and width constraints calculate in runtime.

Codename one - scrollable layout restrictions

I have done my own version of the PropertyCross Demo (provided in their demo section).
The problem I currently face is the size of the "Recent Search" area. While I have a non-scrollable container, I can easily define the preferred height. As the Box Layout adheres to the preferred size, all is well, with the little issue of not being able to scroll it and see more than one result:
recentSearchContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); recentSearchContainer.setPreferredH((int)(this.getContentP‌​ane().getHeight() * 0.1f));
Once I set the container to scrollable, the preferred height gets overwritten and takes up as much space as it needs, taking too much space from the BorderLayout Center piece above it.
How to manipulate the preferred size of scrollable components?
You don't manipulate the preferred size. Scrollables take up more space so if you need them to take up a specific amount of space you need to use the right type of layout which in this case might not be border layout...
Border layout gives NORTH/SOUTH elements their preferred height which might not be what you want. You might want a grid layout which will divide the height 50/50. You might want a table layout where you can define the height in percentages etc.
For those who are interested, here is the solution:
Setup a table layout with a single column and as many rows as you need (similar to box layout y axis or border layout which only north, center and south).
Set the table layout to non-scrollable so it defaults to 100% of your screen.
add the components with height % of the screen they should take up.
those components can be scrollable and will still stick to the height constraint!
// inside a form object, setup the layout
TableLayout tl = new TableLayout(3, 1);
tl.setGrowHorizontally(true);
setScrollable(false);
setLayout(tl);
...
// and add stuff to it
add(tl.createConstraint().heightPercentage(15), labelDesc);
add(tl.createConstraint().heightPercentage(50), compGroup);
add(tl.createConstraint().heightPercentage(35), recentSearchContainer);
Works like a charm!

Why won't the children in my JavaFX HBox grow (SceneBuilder)?

The JavaFX docs for HBox say:
If an hbox is resized larger than its preferred width, by default it will keep children to their preferred widths, leaving the extra space unused. If an application wishes to have one or more children be allocated that extra space it may optionally set an hgrow constraint on the child. See "Optional Layout Constraints" for details.
In the attached image, why isn't the button filling the horizontal width?
Isn't that the same as the HBox.setHgrow(field, Priority.ALWAYS) code the docs refer to.
Select your button as you have done in your screenshot and in those Size boxes in the Layout pane on the right set the Max Width to:
MAX_VALUE
This will stop the maximum size of the button from being bound to the preferred size of the button.
See Oracle's Tips for Sizing and Aligning Nodes
UI controls also provide default minimum and maximum sizes that are based on the typical usage of the control. For example, the maximum size of a Button object defaults to its preferred size because you don't usually want buttons to grow arbitrarily large.
....
To enable all of the buttons to be resized to the width of the VBox pane, the maximum width of each button is set to the Double.MAX_VALUE constant, which enables a control to grow without limit. An alternative to using the maximum value constant is to set the maximum width to a specific value, such as 80.0.
If you use fxml directly, you can use the maxWidth="Infinity", or - just as SceneBuilder does when using MAX_VALUE that is represented by maxWidth="1.7976931348623157E308".
This could look like this (the progress bar is inside a GridPane:
<ProgressBar maxWidth="Infinity" prefWidth="200.0" progress="0.0" GridPane.columnIndex="0" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.valignment="CENTER">

ScrollPane with preferedViewportWidth matching preferedWidth of content

I have a bunch of regions which have a specific preferedWidth set. These Regions are reused visual components in a drag and drop UI.
I want to create a conatainer of these regions and tried to accomplish this with a ScrollPane with an embedded VBox. I want the scrollpane to be wide enough to hold the VBox without horizontal scrolling.
I could figure out the width of the VBox by hand and hardcode the scrollbars width but i would prefere a dynamic solution so that i can style the vbox later. Unfortunately the vbox preferred width is -1 even though its children have a prefered width set.
Also if i try to set the scrollpanes preferedViewportWidth to the width of my regions i get mixed results dependent on the hbarPolicy. If the policy is set to AS_NEEDED the width of the scrollbar is ignored and the scrollbar appears over my regions when it appears.
Any ideas how i get a SrollPane which is wide enough for my regions with and without a vertical scrollbar and possible styling of paddings etc.
Solved my problem by binding the ScrollPane's prefViewportWidthProperty() to the width property of the child.

Resources