CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers 289
The SlidingPaneLayout class implements the use of layout weight parameter constants by using
its nested class SlidingDrawerLayout.LayoutParams. The nested class only contains this one
single android:layout_weight attribute (or property, or parameter), so rather than adding an entire
technical section on this nested class at this point in this chapter, I’ll instead include a section
discussing what the android:layout_weight parameter will do for the LinearLayout container and for
the SlidingPaneLayout container.
UI Spacing in LinearLayout and SlidingPaneLayout: Using Layout Weight
The LinearLayout and SlidingPaneLayout classes are the only UI layout classes which support
assigning a UI element weight to an individual child widget UI element within the UI layout container.
This is done by using the android:layout_weight attribute, along with a floating-point (also known
in Java as a float) numeric value.
This weight attribute, property, or parameter serves to assign a “spacing importance” value to a
child widget UI element. This spacing importance is defined in terms of what numeric percentage of
the total screen space that the UI widget should occupy within the overall UI layout design, which is
usually the entire display screen due to the MATCH_PARENT layout constant.
The layout_weight parameter looks at all of the weights defined across the layout (in the case of
the SlidingPane UI, it would be the two panes), but in the case of a LinearLayout, it could be more
than two child UI design (nested layout containers or widget) elements. The android:layout_weight
parameter uses these numeric weight definitions to determine exactly what percentage of the screen
real estate each UI design element will occupy.
A relatively larger weight value will allow a UI element to be allocated more of the total screen real
estate in the direction (X or Y dimension) for which that weight has been defined for that UI element.
The default setting for the weight parameter, if no weight value is specified, is zero, which represents
the very lowest priority.
If other UI elements have been assigned a higher weight value, then the default value of zero would
be the equivalent of the wrap_content layout constant setting being applied to a UI element and no
additional spacing will be added around that particular UI design element. If you remember, using
wrap_content is like shrink-wrapping your UI widget.
If all of your child UI elements (widgets or nested layouts) specify layout_weight values, then all of
the screen space in the parent ViewGroup (SlidingPaneLayout or LinearLayout) will be allocated to
the children according to the relative proportion between all of the declared weights.
Since floating-point values are required, a logical way to approach this would be to use
“percentage of one” data values. For instance, a floating-point value of 0.25 would represent 25%
of the total screen real estate.
When using this approach to defining screen layout weight percent values, make sure that all of your
floating-point android:layout_weight values add up to an even 1.0 value (which would signify 100%
of the screen area).
As an example, if you have three child Button UI elements within a parent LinearLayout container,
and you set the weight to 1 in two of these, while the other is given no weight parameter (that is, set
to 0), then your third Button UI element without any weight will not expand to fill any UI design space
and will only occupy the area required by its content. This is why I made a “zero weight is equivalent
to the Android wrap_content constant” analogy earlier.