GridLayout


public class GridLayout extends ViewGroup


A layout that places its children in a rectangular grid.

The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices. A grid with N columns has N + 1 grid indices that run from 0 through N inclusive. Regardless of how GridLayout is configured, grid index 0 is fixed to the leading edge of the container and grid index N is fixed to its trailing edge (after padding is taken into account).

Row and Column Specs Children occupy one or more contiguous cells, as defined by their rowSpec and columnSpec layout parameters. Each spec defines the set of rows or columns that are to be occupied; and how children should be aligned within the resulting group of cells. Although cells do not normally overlap in a GridLayout, GridLayout does not prevent children being defined to occupy the same cell or group of cells. In this case however, there is no guarantee that children will not themselves overlap after the layout operation completes. Default Cell Assignment If a child does not specify the row and column indices of the cell it wishes to occupy, GridLayout assigns cell locations automatically using its: orientation, rowCount and columnCount properties. Space Space between children may be specified either by using instances of the dedicated android.widget.Space view or by setting the leftMargin, topMargin, rightMargin and bottomMargin layout parameters. When the useDefaultMargins property is set, default margins around children are automatically allocated based on the prevailing UI style guide for the platform. Each of the margins so defined may be independently overridden by an assignment to the appropriate layout parameter. Default values will generally produce a reasonable spacing between components but values may change between different releases of the platform. Excess Space Distribution GridLayout's distribution of excess space accommodates the principle of weight. In the event that no weights are specified, columns and rows are taken as flexible if their views specify some form of alignment within their groups.

The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.

Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.

To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.

When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.

Interpretation of GONE For layout purposes, GridLayout treats views whose visibility status is GONE, as having zero width and height. This is subtly different from the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked view was alone in a column, that column would itself collapse to zero width if and only if no gravity was defined on the view. If gravity was defined, then the gone-marked view has no effect on the layout and the container should be laid out as if the view had never been added to it. GONE views are taken to have zero weight during excess space distribution.

These statements apply equally to rows as well as columns, and to groups of rows or columns.

See GridLayout.LayoutParams for a full description of the layout parameters used by GridLayout.

name android:orientation

name android:rowCount

name android:columnCount

name android:useDefaultMargins

name android:rowOrderPreserved

name android:columnOrderPreserved

Summary

Nested types

public abstract class GridLayout.Alignment

Alignments specify where a view should be placed within a cell group and what size it should be.

Layout information associated with each of the children of a GridLayout.

public class GridLayout.Spec

A Spec defines the horizontal or vertical characteristics of a group of cells.

Constants

static final int

This constant is an alignmentMode.

static final int

This constant is an alignmentMode.

static final GridLayout.Alignment

Indicates that a view should be aligned with the baselines of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be aligned with the bottom edges of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be centered with the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be aligned with the end edges of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should expanded to fit the boundaries of its cell group.

static final int

The horizontal orientation.

static final GridLayout.Alignment

Indicates that a view should be aligned with the left edges of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be aligned with the right edges of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be aligned with the start edges of the other views in its cell group.

static final GridLayout.Alignment

Indicates that a view should be aligned with the top edges of the other views in its cell group.

static final int
UNDEFINED = -2147483648

The constant used to indicate that a value is undefined.

static final int

The vertical orientation.

Public constructors

GridLayout(Context context, AttributeSet attrs)
GridLayout(Context context, AttributeSet attrs, int defStyle)

Public methods

GridLayout.LayoutParams
int

Returns the alignment mode.

int

Returns the current number of columns.

int

Returns the current orientation.

Printer

Return the printer that will log diagnostics from this layout.

int

Returns the current number of rows.

boolean

Returns whether or not this GridLayout will allocate default margins when no corresponding layout parameters are defined.

boolean

Returns whether or not column boundaries are ordered by their grid indices.

boolean

Returns whether or not row boundaries are ordered by their grid indices.

void
void
setAlignmentMode(int alignmentMode)

Sets the alignment mode to be used for all of the alignments between the children of this container.

void
setColumnCount(int columnCount)

ColumnCount is used only to generate default column/column indices when they are not specified by a component's layout parameters.

void
setColumnOrderPreserved(boolean columnOrderPreserved)

When this property is true, GridLayout is forced to place the column boundaries so that their associated grid indices are in ascending order in the view.

void
setOrientation(int orientation)

GridLayout uses the orientation property for two purposes:

void

Set the printer that will log diagnostics from this layout.

void
setRowCount(int rowCount)

RowCount is used only to generate default row/column indices when they are not specified by a component's layout parameters.

void
setRowOrderPreserved(boolean rowOrderPreserved)

When this property is true, GridLayout is forced to place the row boundaries so that their associated grid indices are in ascending order in the view.

void
setUseDefaultMargins(boolean useDefaultMargins)

When true, GridLayout allocates default margins around children based on the child's visual characteristics.

static GridLayout.Spec
spec(int start)

Return a Spec, spec, where:

static GridLayout.Spec
spec(int start, GridLayout.Alignment alignment)

Return a Spec, spec, where:

static GridLayout.Spec
spec(int start, int size)

Return a Spec, spec, where:

static GridLayout.Spec
spec(int start, float weight)

Equivalent to: spec(start, 1, weight).

static GridLayout.Spec
spec(int start, GridLayout.Alignment alignment, float weight)

Equivalent to: spec(start, 1, alignment, weight).

static GridLayout.Spec
spec(int start, int size, GridLayout.Alignment alignment)

Equivalent to: spec(start, size, alignment, 0f).

static GridLayout.Spec
spec(int start, int size, float weight)

Equivalent to: spec(start, 1, default_alignment, weight) - where default_alignment is specified in android.widget.GridLayout.LayoutParams.

static GridLayout.Spec
spec(int start, int size, GridLayout.Alignment alignment, float weight)

Return a Spec, spec, where:

Protected methods

boolean
GridLayout.LayoutParams
GridLayout.LayoutParams
void
onLayout(boolean changed, int left, int top, int right, int bottom)
void
onMeasure(int widthSpec, int heightSpec)

Inherited Constants

From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

Inherited methods

From android.view.View
void
void
void
ViewPropertyAnimator
void
void
boolean
void
void

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
canScrollHorizontally(int direction)
boolean
canScrollVertically(int direction)
final void
void
final void
boolean
void
void
static int
combineMeasuredStates(int curState, int newState)
int
int
int
void
WindowInsets
int
int
int
AccessibilityNodeInfo
void
void

This method is deprecated.

boolean
boolean
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean
dispatchNestedPreFling(float velocityX, float velocityY)
boolean
dispatchNestedPrePerformAccessibilityAction(
    int action,
    Bundle arguments
)
boolean
dispatchNestedPreScroll(
    int dx,
    int dy,
    int[] consumed,
    int[] offsetInWindow
)
boolean
dispatchNestedScroll(
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int[] offsetInWindow
)
boolean
void
draw(Canvas canvas)
void
drawableHotspotChanged(float x, float y)
final OnBackInvokedDispatcher
final T
<T extends View> findViewById(int id)
final T
<T extends View> findViewWithTag(Object tag)
boolean

This method is deprecated.

void
forceHasOverlappingRendering(boolean hasOverlappingRendering)
void
void
generateDisplayHash(
    String hashAlgorithm,
    Rect bounds,
    Executor executor,
    DisplayHashResultCallback callback
)
static int
View.AccessibilityDelegate
int
AccessibilityNodeProvider
CharSequence
int
int
String
String
float
Animation
Matrix
IBinder
int[]
Map<IntegerInteger>
String[]
final AutofillId
int
AutofillValue
Drawable
BlendMode
ColorStateList
PorterDuff.Mode
int
final int
float
int
float
Rect
boolean
final boolean
final ContentCaptureSession
CharSequence
final Context
ContextMenu.ContextMenuInfo
final boolean
static int
getDefaultSize(int size, int measureSpec)
Display
final int[]
Bitmap

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
long
float
int
boolean
boolean
int
ArrayList<View>
getFocusables(int direction)
void
Drawable
int
BlendMode
ColorStateList
PorterDuff.Mode
boolean
getGlobalVisibleRect(Rect r, Point globalOffset)
Handler
float
float
float
float
Runnable
final boolean
final int
void
getHitRect(Rect outRect)
int
int
Drawable
Drawable
int
getId()</