Mallow's Blog

Advantages and implementation of FlexboxLayout in Android

Flexbox Layout is same as CSS Flexible Layout module in android. Flexbox can be judge as advance form on LinearLayout, both layout are aligned sequentially. The difference is that, Flexbox has feature of wrapping. For using FlexboxLayout in your app, you need to add gradle dependency in you app gradle file:

dependencies {



compile ''


Let us see in detail about the cases where Flexbox is useful:

1. In Flexbox when you arrange items in horizontal manner with attribute (flexWrap=”Wrap”). If there is not enough space left in current line, then It take to the new line.


2.To achieve this, You need to set multiple layout design for various screen aspects (like layout-600dp, layout-720dp, layout-1020dp). But by using Flexbox, you can reduce the number of layouts design.

You can use this Flexbox layout instead of linearlayout as below.


By this child aligned to new line instead of overflowing its parent.


3. Can also pad space in between them when you have space at the end, but can’t able to load a new item. Then attribute (layout_flexgrow=”1″), will make the line between items with equal space.



4. Flexbox with RecyclerView,

A latest version of Alpha version, the Flexbox extends RecyclerView.LayoutManager, we can use the scrollable container in much more memory-efficient way.

Real world examples for RecyclerView with Flexbox are, GooglePhotos and NewStand, where there will be lots of cards and need to be handle various width of items.

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();


void bindTo(Drawable drawable) {


ViewGroup.LayoutParams lp = mImageView.getLayoutParams();

if (lp instanceof FlexboxLayoutManager.LayoutParams) {

FlexboxLayoutManager.LayoutParams flexboxLp =

(FlexboxLayoutManager.LayoutParams) mImageView.getLayoutParams();



//Your own code



5. FlexDirection

The direction of its children is controlled by FlexDirection.

– row, row_reverse, column and column_reverse

i. row : Its children will aligned in LTR direction (Left to Right).

ii. row_reverse : Its children will aligned in RTL direction (Right to Left).

iii. column : Its children will aligned one below another starting from Top of the layout.

iv. column_reverse : Its children will aligned one above another starting from Bottom of the layout.


6. JustifyContent

Child items are positioned along the main axis is controlled by justifyContent similar to flexDirection, But flexDirection controls the “order of the child items” whereas JustifyContent controls the “relative positioning”.

– flex_start, flex_end, center, space_between and space_around

i. flex_start : Start its child from a new line when its wrap in LTR direction.

ii. flex_end : Start its child from a new line when its wrap in RTL direction.

iii. center : Start its child from a new line when its wrap from its center.

iv. space_between : will provide equal space in between its child.

v. space_around : will provide equal space in between its child including padding in first and last child in a row.


7. AlignItems

AlignItems controls the positioning and sizing of items along the cross axis. The possible values are stretch, flex_start, flex_end, center, and baseline.


8. AlignContent

There is a thin line of difference in between AlignItems and AlignContent. AlignItems controls how items are positioned within their own line, Whereas AlignContent controls the line itself. Similar to TableRow within TableLayout, Using “alignContent” is similar to applying layout attributes to TableRow, whereas “alignItems” is similar to applying layout attributes to the child views representing the individual cells in table. The possible values are stretch, flex_start, flex_end, center, space_between, and space_around.

In this blog, you have seen pros of FlexboxLayout which overcomes the cons of LinearLayout. In our next blog we can see how to implement FlexboxLayout in RecyclerView.


For reference :


Pangaj JG
Android Developer,
Mallow Technologies


  1. Priya

    How can I count number of rows filled in FlexboxLayout

  2. Navdeep Singh

    is it Compatible with older APIs?

Leave a Comment

Your email address will not be published. Required fields are marked *