Understanding Constraint Layout and its features
Posted By : Daljeet Singh | 31-Jul-2018
ConstraintLayout was introduced to reduce the nesting and complexity of views while creating a layout in android.Using ConstraintLayout you can define a lot of common layouts inside a single parent.ConstraintLayout saves us from the performance issues that accompany a RelativeLayout while also letting us position views with respect to each other.
You can use ConstraintLayout by adding the following line to your app's build.gradle :
Here's a sample layout created using ConstraintLayout as its parent :
The above layout allows a user to center two views equally without the use of weights,which otherwise would have created a performance overhead while rendering.
Some of the core features of a ConstraintLayout are :
- Barrier : A barrier is nothing but an invisible view which contains a reference to the views against whom we want to create a barrier.If the size of one of the views grows,the barrier adjusts its size against the largest height or width of the items referenced.A Barrier can be either horizontal or vertical and can be positioned at the left,right,top or bottom of the items referenced.The rest of the views can then constrain themselves to the barrier.
- Guidelines : Guidelines assist you in designing layouts.We can align views to a guideline which helps in simplifying layouts,primarily if the same margin values have to be used on a number of views.Guidelines can be provided in dpi from the start or end of the screen or can be specified as a percentage of the screen width. Views in the layout can constrain themselves to the guideline.
- Chains : Chains allow us to manipulate the space between views and the way they use the space.In order to form a chain,choose views that you want to include in the chain and then select chain->Create Horizontal/Vertical Chain.You can then cycle through different chain modes.The XML for constructing a chain is different in the sense that all views have constraints defined on themselves.