![]() |
VOOZH | about |
ConstraintLayout is the most advanced layout in Android that lets you create complex and responsive UIs while minimizing nested views due to its flat view hierarchy. ConstraintLayout is similar to that of other View Groups which we have seen in Android such as RelativeLayout, LinearLayout,and many more. In this article, we will take a look at using ConstraintLayout in Android.
Attributes | Description |
|---|---|
| android:id | Assigns a unique ID to the layout. |
| app:layout_constraintBottom_toBottomOf | Constraints the view with respect to the bottom position. |
| app:layout_constraintLeft_toLeftOf | Constraints the view with respect to the left position. |
| app:layout_constraintRight_toRightOf | Constraints the view with respect to the right position. |
| app:layout_constraintTop_toTopOf | Constraints the view with respect to the top position. |
app:layout_constraintHeight_max | Sets the max height of view according to the constraints. |
app:layout_constraintHeight_min | Sets the height of the view according to the constraints. |
app:layout_constraintHorizontal_weight | Sets the weight horizontally of a particular view same as linear layouts. |
app:layout_constraintVertical_weight | Sets the weight vertically of a particular view same as linear layouts. |
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Navigate to the app > Gradle Scripts > build.gradle (Module level) file and add the below dependency to it in the dependencies section.
implementation "androidx.constraintlayout:constraintlayout:2.2.0"Now sync your project and we will move towards working with activity_main.xml.
Navigate to the app > res > layout > activity_main.xml and add the below code to that file.
In the above code, we use constraints to position the TextView at the centre of the screen. Constraints align child views relative to the parent or other child views within the parent layout. Below is the expected Design and Blueprint of the xml layout:
As we are working only with layouts, therefore it isn't necessary to modify the code in MainActivity.kt. Now, we can build and run the app by pressing "Shift+F10" on the keyboard or clicking the "run" button on the top bar of the screen. Below is the expected output from the above code:
Constraint Layout | Linear Layout |
|---|---|
Based on flat hierarchy to avoid multiple nesting. | Can be nested with complex layouts. |
Better performance for complex layouts due flat hierarchy. | Slower performance in complex layouts due to nested hierarchy. |
In ConstraintLayout, we can position our UI components in any sort of order whether it may be horizontal or vertical. | But in the case of Linear Layout, we can only arrange our UI components either in a horizontal or in a vertical manner. |
In Constraint Layout we have to arrange this UI component manually. | In Linear Layout provides usability with which we can equally divide all the UI components in a horizontal or vertical manner using weight sum |
In Constraint layout if the UI component is not Constrained then the UI will not look the same as that of in design editor. | In Linear Layout the UI which is actually seen in the Design editor of Android Studio will be the same as that we will get to see in the app |
Constraint Layout | RelativeLayout |
|---|---|
Based on flat hierarchy to avoid multiple nesting. | Can be deeply nested with complex layouts. |
High flexibility due to the use of constraints, chains and guidelines. | Lower flexibility due to relative positioning. |
In Constraint Layout, we have to add constraints to the view on all four sides | In Relative Layout we can simply align our UI component relative to its ID using the ids of UI components. |
In Constraint layout if the UI component is not Constrained then the UI will not look same as that of in design editor. | In Relative Layout, the UI which is actually seen in the Design editor of Android Studio will be the same as that we will get to see in the app |
Constraint Layout | GridLayout |
|---|---|
Creates complex, responsive and dynamic UIs | Creates simple, grid-based UIs (e.g. Image Gallery) |
Constraint Layout we can align UI components according to the requirement. | In Grid Layout the UI components are only arranged in Grid Manner and we cannot arrange the UI components according to requirement |
Constraint layout if the UI component is not Constrained then the UI will not look same as that of in design editor. | In Grid Layout, the UI which is actually seen in the Design editor of Android Studio will be the same as that we will get to see in the app |
There are various features associated with ConstraintLayout in Android mentioned below:
Note :To access the full android application using ConstraintLayout, check this repository:ConstraintLayout in Android Application