Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why go for Constraints layout as we already have Relative Layout? [duplicate]

Why RelativeLayout replaced to ConstraintLayout in the default layout file of an android empty activity. I need to know why we should use ConstraintLayout, what other benefits it provides to us.

like image 736
Manish Patiyal Avatar asked Jun 23 '16 13:06

Manish Patiyal


People also ask

Why constraint layout is better than relative layout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

Why do we prefer constraint layout?

Advantages of using ConstraintLayout in AndroidIt helps to improve the UI performance over other layouts. With the help of ConstraintLayout, we can control the group of widgets through a single line of code. With the help of ConstraintLayout, we can easily add animations to the UI components which we used in our app.

Should I use relative layout or constraint layout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).

Which is better constraint layout or linear layout?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.


1 Answers

As provided by Xaver Kapeller - "The main purpose of the ConstraintLayout is to fix problems with the RelativeLayout, and it does it so well. You can do so many things that were impossible with a RelativeLayout. And you can simplify your layout like you never could before. Additionally, it fixes a long-standing performance issue of the RelativeLayout. The double taxation during the measure/layout phase. So we get better performance, more versatility and much simpler layouts in one nice package."

An additional benefit of using it is that it allows you to create your layouts using a drag and drop style editor visually in a way RelativeLayout couldn't similar to Interface Builder in Xcode.

Constraint Layout: This new layout is a flexible layout manager for your app that allows you to create dynamic user interfaces without nesting multiple layouts. It is distributed as a support library that is tightly coupled with Android Studio and backwards compatible to API Level 9.

At first glance, Constraint Layout is similar to RelativeLayout. However, the Constraint Layout was designed to be used in Studio and it can efficiently express your app design so that you rely on fewer layouts like LinearLayout, FrameLayout, TableLayout, or GridLayout. Lastly, with the built-in automatic constraints inference engine. You can freely design your UI to your liking and let Android Studio do the hard work.

like image 101
egfconnor Avatar answered Sep 18 '22 07:09

egfconnor