DID YOU KNOW about ConstraintLayout?
ConstraintLayout is a new type of layout introduced by Android, similar to RelativeLayout but more flexible, and it is used to position UI elements relative to other UI elements by using constraints.
A constraint represent the rules we have to apply in order to position our UI elements relative to each other.
- ConstraintLayout has backwards compatibility (it is available in Support Library compatible with Android 2.3 and higher)
- When you create a new project, ConstraintLayout is used now in the template instead of RelativeLayout
- ConstraintLayout can be used directly from xml, but it is recommended to use the new Layout Editor
- Layout editor is available from Android Studio 2.2 Preview and was build especially for this new type of layout
- constraints can be created:
- by using Autoconnect (before, the layout editor started with Autoconnect enabled, but from Android Studio 2.2 Preview 5, it is now disabled by default. Also, the icon will be visible only if you use ConstraintLayout).
- by using Inference
- the difference between Autoconnect and Inference is that, Autoconnect creates constraints for the widget being currently moved (the current widget is constrained by other widgets, but other widgets won’t be constrained by the widget currently being moved) , whereas Inference creates constraints among all elements in a layout.
- constraints can be defined for one or more sides of a view by connecting the view to:
- an anchor point on another view
- an edge of the layout
- an invisible guideline
Rules for the Constraint System
- anchors on different axis cannot be connected (such as top and left anchor)
- baseline constraint can only be constrained to another baseline
- constraints resulting in a cycle are not permitted
Search In Blog
Want to donate?
Recent Posts: My Android Solutions
If you ever wondered how to read and parse a simple JSON file stored in the assets directory of your Android project, here is a way to do it.
In this tutorial I will create a simple Sectioned RecyclerView that will show 2 sections and a few items in each section and how to move an item from one section to another. Note: For RecyclerView I used AndroidX imports and not android.support.v7.widget.RecyclerView! Setup We need to import the recycler view from AndroidX. The build.gradle…
Recently, I had to add a perfect square CardView and I wanted to achieve this without hardcoding its width and height. I happily discovered the power of ConstraintLayout and this is how I managed to create a perfect square which should work on every dimension. In my project I created a RecyclerView with 2 columns,…