Open In App

How to Use Flows in Android ConstraintLayout to Build Complex Layouts?

If you are an android developer you already know how difficult it to create a GridLayout. Also, layouts like GridLayout and LinearLayout are not that light weight for your tiny mobile phone display, they cause our phone’s display to function slow and because of this user experience of application users is not that good. Due to these types of problems, Google has introduced a feature known as “flows” in the constraint layout 2.0 and above. Flows are used to make the layouts like GridLayouts and LinearLayouts in a much easier and efficient form. Also, it is lightweight for your android phone display. As flows make our work easy, it is easy to implement in your android application. Following are the steps how you can create layouts like GridLayouts with flows.

Step by Step Implementation

Step 1: Open your Android Studio and click on start new project, give a suitable name and package name and select API levels shown in the below image and click on the finish button. After the successful gradle sync your good to go to the next step.



Step 2: Drag and drop the views in the constraint layout section as shown in the below image. Here we have used only buttons for reference, you can add any type of views in the constraint layout. Make sure that your root layout must be a constraint layout. Also, change the layout width of buttons to be match constraint (0dp) so that buttons will occupy the maximum space as needed in this project. Do not constraint your views in a constrained layout.



Step 3: Click on the guideline section and there you will get the add flow option, click on it and your flow is added to the component tree section. Now select the flow and change the layout width to be match constraint (0dp) and also add the constraint to flow according to you.

Adding flow into the component tree

Step 4: Select all the buttons using “ctrl+ left mouse click” and drag all images into the flow. This will give all the reference ID to the flow for the actions to perform on that buttons using the flow. You can also specify a reference ID in the flow attribute name as “constraint_referenced_ids”, just pass the ID names of buttons separated by commas.

Selecting and adding all buttons to flow

Step 5: Now we have to set some important attributes of flows to make the actual design like grid layout. Following are the important attributes for flow to work.

Example of aligned mode

Article Tags :