Differences between ConstraintLayout and RelativeLayout
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
What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
LinearLayout
means you can align views one by one (vertically/ horizontally).
RelativeLayout
means based on relation of views from its parents and other views.
ConstraintLayout
is similar to a RelativeLayout in that it uses relations to position and size widgets, but has additional flexibility and is easier to use in the Layout Editor.
WebView
to load html, static or dynamic pages.
FrameLayout
to load child one above another, like cards inside a frame, we can place one above another or anywhere inside the frame.
deprecated - AbsoluteLayout
means you have to give exact position where the view should be.
For more information, please check this address https://developer.android.com/guide/topics/ui/declaring-layout#CommonLayouts
Constraint layout vs Relative layout
It is not much different than other layouts you are probably already using (like RelativeLayout or LinearLayout). The attributes are very similar to the ones used with Relative Layout.
This is primarily designed as a visually oriented tool. The visual editor is intended to be the main way developers interact with their layouts.
The main goal of this new layout, is to help developers create complex layouts that are optimized to rendez quickly. In fact, its aim is to reduce layout hierarchies induced by other layout types.
ConstraintLayout vs Traditional Layouts
I am just wondering when to choose the CoordinatorLayout over
"Traditional" Layouts or are they (or at least some of them) going to
be deprecated?
So ConstraintLayout
is useful, but (for now) it is not required for Android app development, any
more than LinearLayout
and RelativeLayout
are. And, because ConstraintLayout
is a
library, you will need to take some additional steps to add it to your project (com.android.support.constraint:constraint-layout
artifact in your
dependencies closure of your module’s build.gradle file), and it
adds ~100KB to the size of your Android app.
I am personally so accustomed to writing layouts by hand in xml so
transitioning to the ConstraintLayout is very hard for me.
Drag and drop GUI builder
Google is trying to make life easier to developers and make they work faster 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 the developer
releases the mouse button and completes the drop.
With LinearLayout
adding widgets is easy. With RelativeLayout
is difficult for GUI bulder to handle drag-drop and probably you will have to dig inside the XML code to get things done.ConstraintLayout
was created with GUI building in mind, to make it a bit easier to
infer the right rules based upon where the developer happens to drop a widget.
Recomputing size and position
Changing the details of a widget often cause
the sizes to have to be recomputed. For example nne change in TextView
might
cause that whole hierarchy to go through re-size/re-position work. If you have container inside container which is inside another container etc., means that parents re-size/re-position their children and that can be very
expensive for deep hierarchies.
So
Does the ConstraintLayout have better performance then a nested
Layout?
Yes, ConstraintLayout
is being designed with performance in mind, trying to eliminate
as many pass scenarios as possible and by trying to eliminate the need for
deeply-nested view hierarchies.
Huh,
for more you can take a look in a book about android development from CommonsWare.
There using ConstraintLayout
is explained in more details with comparation examples with another containers like LinearLayout
, RelativeLayout
etc. Really anatomy of android development.
Android Studio - Constraint Layout Vs Relative Layout Which Is Better?
" but when I create an Activity in a Constraint Layout, the items get
misplaced during runtime why is that so?"
This happens because you must not be adding all the required constraints for your views.
For example: If your view has only it's top aligned to another view, on rendering, it may move anywhere horizontally since it's left or right isn't aligned.
I suggest that you look into the official documentation and examples. You could also search for YouTube tutorials on ConstraintLayout.
Constraint Layout instead of Relative Layout
ConstraintLayout
does all thatRelativeLayout
does, and more- It's supported starting from API level 9 (Gingerbread) -- ie. 99.9% of devices
- Yes. You can edit the XML by hand too, but the editor in 2.2 is recommended
- It depends what you want to do -- using dimens might still be useful (for example, you might want roughly the same layout, but with different margins).
Related Topics
Android Device Not Shown with Adb
Android: How to Periodically Send Location to a Server
Android Mediaplayer Error (1, -2147483648)
Java.Lang.Classnotfoundexception on Working App
Android - How to Unregister a Receiver Created in the Manifest
Google Play Services in Emulator, Implementing Google Plus Login Button etc
Connectivitymanager Getnetworkinfo(Int) Deprecated
Accessing Sharedpreferences Through Static Methods
System Is Returning Error 127 When Called from C++ in Linux
Android Command Line Tools Sdkmanager Always Shows: Warning: Could Not Create Settings
Schedule a Work on a Specific Time with Workmanager
Is There Any Reason to Use the Support.V4 Library in Android
Get Current Location of User in Android Without Using Gps or Internet
How to Check If an Activity Is the Last One in the Activity Stack for an Application