# Class GridLayout

java.lang.Object
com.codename1.ui.layouts.Layout
com.codename1.ui.layouts.GridLayout

public class GridLayout extends Layout

The components are arranged in a grid based on available space, all cells in the grid are given exactly the same size which matches the largest preferred size or available space. The main use case for this layout is a grid of icons e.g. like one would see in the iPhone home screen.

If the number of rows * columns is smaller than the number of components added a new row is implicitly added to the grid. However, if the number of components is smaller than available cells (won't fill the last row) blank spaces will be left in place.

In this example we can see that a 2x2 grid is used to add 5 elements, this results in an additional row that's implicitly added turning the grid to a 3x2 grid implicitly and leaving one blank cell.

    Form hi = new Form("Grid Layout 2x2", new GridLayout(2, 2));
    hi.add(new Label("First")).
        add(new Label("Second")).
        add(new Label("Third")).
        add(new Label("Fourth")).
        add(new Label("Fifth"));

When we use a 2x4 size ratio we would see elements getting cropped as we do here. The grid layout uses the grid size first and doesn't pay too much attention to the preferred size of the components it holds.

Grid also has an autoFit attribute that can be used to automatically calculate the column count based on available space and preferred width. This is really useful for working with UI's where the device orientation might change.

There is also a terse syntax for working with a grid that has two versions, one that uses the "auto fit" option and another that accepts the column names. Heres a sample of the terse syntax coupled with the auto fit screenshots of the same code in two orientations:

    GridLayout.encloseIn(new Label("First"),
        new Label("Second"),
        new Label("Third"),
        new Label("Fourth"),
        new Label("Fifth")));

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GridLayout(int columns)`
  Creates a new instance of GridLayout with the given columns, rows is set to 1 but will implicitly grow if more components are added
  `GridLayout(int rows, int columns)`
  Creates a new instance of GridLayout with the given rows and columns
  `GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns)`
  Creates a new instance of GridLayout with the given rows and columns

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static GridLayout`
  `autoFit()`
  Returns a grid layout that implicitly auto-fits to width in term of number of columns
  `static Container`
  `encloseIn(int columns, Component... cmp)`
  Creates a new container with the grid layout and the components added to it
  `static Container`
  `encloseIn(Component... cmp)`
  Creates a new container with an auto fit grid layout and the components added to it
  `boolean`
  `equals(Object o)`
  `int`
  `getColumns()`

  ###### Returns

  `Dimension`
  `getPreferredSize(Container parent)`
  Returns the container preferred size
  `int`
  `getRows()`

  ###### Returns

  `int`
  `hashCode()`
  `boolean`
  `isAutoFit()`
  Auto fits columns/rows to available screen space
  `boolean`
  `isFillLastRow()`
  When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.
  `boolean`
  `isHideZeroSized()`
  When set to true components that have 0 size will be hidden and won't occupy a cell within the grid.
  `void`
  `layoutContainer(Container parent)`
  Layout the given parent container children
  `boolean`
  `obscuresPotential(Container parent)`
  Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that.
  `void`
  `setAutoFit(boolean autoFit)`
  Auto fits columns/rows to available screen space
  `void`
  `setFillLastRow(boolean fillLastRow)`
  When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.
  `void`
  `setHideZeroSized(boolean hideZeroSized)`
  When set to true components that have 0 size will be hidden and won't occupy a cell within the grid.
  `String`
  `toString()`

  ### Methods inherited from class Layout

  `addLayoutComponent, cloneConstraint, getChildrenInTraversalOrder, getComponentConstraint, isConstraintTracking, isOverlapSupported, overridesTabIndices, removeLayoutComponent, updateTabIndices`

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### GridLayout

    public GridLayout(int rows, int columns)

    Creates a new instance of GridLayout with the given rows and columns

    ###### Parameters

    - `rows`: - number of rows.

    - `columns`: - number of columns.

    ###### Throws

    - `IllegalArgumentException`: if rows < 1 or columns < 1

  - ### GridLayout

    public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns)

    Creates a new instance of GridLayout with the given rows and columns

    ###### Parameters

    - `rows`: - number of rows.

    - `columns`: - number of columns.

    - `landscapeRows`: - number of rows when in landscape mode

    - `landscapeColumns`: - number of columns when in landscape mode

    ###### Throws

    - `IllegalArgumentException`: if rows < 1 or columns < 1

  - ### GridLayout

    public GridLayout(int columns)

    Creates a new instance of GridLayout with the given columns, rows is set to 1 but will implicitly grow if more components are added

    ###### Parameters

    - `columns`: - number of columns.

    ###### Throws

    - `IllegalArgumentException`: if rows < 1 or columns < 1

- ## Method Details

  - ### autoFit

    public static GridLayout autoFit()

    Returns a grid layout that implicitly auto-fits to width in term of number of columns

    ###### Returns

    a grid layout that automatically adapts its size

  - ### encloseIn

    public static Container encloseIn(Component... cmp)

    Creates a new container with an auto fit grid layout and the components added to it

    ###### Parameters

    - `cmp`: the components

    ###### Returns

    a new container

  - ### encloseIn

    public static Container encloseIn(int columns, Component... cmp)

    Creates a new container with the grid layout and the components added to it

    ###### Parameters

    - `columns`: the number of columns for the grid

    - `cmp`: the components

    ###### Returns

    a new container

  - ### layoutContainer

    public void layoutContainer(Container parent)

    Layout the given parent container children

    ###### Parameters

    - `parent`: the given parent container
    Specified by:
      `layoutContainer` in class `Layout`

  - ### getPreferredSize

    public Dimension getPreferredSize(Container parent)

    Returns the container preferred size

    ###### Parameters

    - `parent`: the parent container

    ###### Returns

    the container preferred size

    Specified by:
      `getPreferredSize` in class `Layout`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### getRows

    public int getRows()

    ###### Returns

    the rows

  - ### getColumns

    public int getColumns()

    ###### Returns

    the columns

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Layout`

  - ### hashCode

    public int hashCode()
    Description copied from class: `Layout`
    Overrides:
      `hashCode` in class `Layout`

  - ### isFillLastRow

    public boolean isFillLastRow()

    When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.

    ###### Returns

    the fillLastRow

  - ### setFillLastRow

    public void setFillLastRow(boolean fillLastRow)

    When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.

    ###### Parameters

    - `fillLastRow`: the fillLastRow to set

  - ### isAutoFit

    public boolean isAutoFit()

    Auto fits columns/rows to available screen space

    ###### Returns

    the autoFit

  - ### setAutoFit

    public void setAutoFit(boolean autoFit)

    Auto fits columns/rows to available screen space

    ###### Parameters

    - `autoFit`: the autoFit to set

  - ### obscuresPotential

    public boolean obscuresPotential(Container parent)

    Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that. This method doesn't take padding/margin etc. into account since that is checked by the caller

    ###### Parameters

    - `parent`: parent container

    ###### Returns

    true if there is a chance that this layout manager can fully obscure the background, when in doubt return false...

    Overrides:
      `obscuresPotential` in class `Layout`

  - ### isHideZeroSized

    public boolean isHideZeroSized()

    When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.

    ###### Returns

    the hideZeroSized

  - ### setHideZeroSized

    public void setHideZeroSized(boolean hideZeroSized)

    When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.

    ###### Parameters

    - `hideZeroSized`: the hideZeroSized to set