Class MasonryLayout

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

public class MasonryLayout extends Layout

Arranges components in equal width columns, putting each one at the top of whichever column is shortest so far: a masonry, staggered or Pinterest-style grid, where tiles keep their own heights and the columns do not line up.

Tiles of different heights packed into two columns

Form form = new Form("Gallery", new BorderLayout());
Container feed = new Container(new MasonryLayout(2));
int[] heights = {240, 160, 300, 200, 180, 260};
for (int h : heights) {
    // stands in for a downloaded photo
    feed.add(new ScaleImageLabel(Image.createImage(200, h, 0xff90caf9)));
}
feed.setScrollableY(true);
form.add(BorderLayout.CENTER, feed);
form.show();

Each component is as wide as its column and as tall as it prefers at that width. Order is kept in the sense that every component is placed after the ones before it, so the top of the grid reads in order. TableLayout and GridLayout differ by aligning every row to its tallest cell.

#setMinimumColumnWidth(float) makes the column count follow the width instead: as many columns as fit, never fewer than the count given.

  • Constructor Details

    • MasonryLayout

      public MasonryLayout()
      A layout with two columns.
    • MasonryLayout

      public MasonryLayout(int columns)

      A layout with a fixed number of columns.

      Parameters
      • columns: the column count, at least 1
  • Method Details

    • getColumns

      public int getColumns()

      The column count, or the minimum when columns follow the width.

      Returns

      the column count

    • setColumns

      public void setColumns(int columns)

      Sets the column count.

      Parameters
      • columns: at least 1
    • setMinimumColumnWidth

      public void setMinimumColumnWidth(float mm)

      Makes the column count follow the width: as many columns at least this wide as fit, and never fewer than #getColumns().

      Parameters
      • mm: the narrowest column in millimetres, or 0 for a fixed count
    • getMinimumColumnWidth

      public float getMinimumColumnWidth()

      The narrowest column when columns follow the width.

      Returns

      millimetres, 0 for a fixed count

    • getGap

      public float getGap()

      The space between columns and between tiles of a column.

      Returns

      millimetres, 1 by default

    • setGap

      public void setGap(float mm)

      Sets the space between columns and between tiles of a column, added to the components' own margins.

      Parameters
      • mm: millimetres
    • columnsFor

      public int columnsFor(int width)

      The number of columns used at a given content width.

      Parameters
      • width: the width available, in pixels
      Returns

      the column count

    • layoutContainer

      public void layoutContainer(Container parent)
      Description copied from class: Layout

      Layout the given parent container children

      Parameters
      • parent: the given parent container
      Specified by:
      layoutContainer in class Layout
    • getPreferredSize

      public Dimension getPreferredSize(Container parent)
      Description copied from class: Layout

      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()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object