I am confused about the difference between ConstraintLayout
and RelativeLayout
. Could someone please tell me the exact differences between them?
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.
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.
Android Layout Types Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.
RelativeLayout based on relation of views. It is a layout manager that helps you arrange your UI elements based on some rule. You can specify things like: align this to parents left edge, place this to the left/right of this elements etc. FrameLayout allows placements along Z-axis.
The intention of ConstraintLayout
is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.
The Rules are similar to RelativeLayout
, for example setting the bottom edge to the bottom of some other view.
app:layout_constraintBottom_toBottomOf="@+id/view1"
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.
app:layout_constraintHorizontal_bias="0.33" <!-- from 0.0 to 1.0 -->
app:layout_constraintVertical_bias="0.53" <!-- from 0.0 to 1.0 -->
The Baseline handle (a long pipe with rounded corners, below the circle handle) is used to align the content of the view with another view reference.
Square handles (on each corner of the view) are used to resize the view in dps.
This is totally opinion based and my impression of ConstraintLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With