Differences between ConstraintLayout and RelativeLayout is the most common question asked in an interview with the experienced android developer. The top differences are explained in this tutorial.
Flat view hierarchy – no nested view groups
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).
Drag and drop GUI builder
In Android Studio you can drag and drop GUI components like TextView, Button, TextInputLayout, etc. So now it makes life easier for developers and makes them work faster UI development and more productive so they continue improving drag-drop GUI builder. However drag-and-drop gestures, the developer is only providing you with X/Y coordinates of a widget, based on where you release the mouse button and completes the drop.
With RelativeLayout
is difficult for GUI builder to handle drag-drop and probably you will have to dig inside the XML code to get things done.
But in ConstraintLayout have an option to constraint applying by the use of Blueprint and Visual Editor tool which makes it easy to design a page.
Recomputing size and position
Each changes the detail of a widget often cause the sizes to have to be recomputed. Let’s take an example a change in EditText might starting point that a whole hierarchy to go through re-size and re-position work. If the application UI (user interface) has a container inside the container which is inside another container etc. Means that parents re-size or re-position their children and that can be very expensive (rework again on the user interface) for deep hierarchies.
Questions In mind [Interview Questions]?
1. Does the ConstraintLayout have better performance than a nested Layout?
Yes, ConstraintLayout
has designed with performance optimization in mind, more effective, easy use, and trying to eliminate as many pass scenarios as possible. This is done by eliminating the deeply-nested view hierarchies with flat view hierarchies.
2. Can we replace RelativeLayout with ConstraintLayout completly?
Yes, you can completely replace RelativeLayout with ConstraintLayout. ConstraintLayout
does all that RelativeLayout
does, and more.
3. Do we don’t need to write many dimens.xml for all screen resolution to have a responsive app?
It depends on what you want to do – using dimensions might still be useful, for example- you might want the same layout but with different margins for different sizes of devices. That time you have to add many dimen.xml resource files.
Bonus: Must read this tutorial for much better understanding.