Open In App

ConstraintLayout in Android

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. 

Important Attributes of ConstraintLayout 

Attributes

Description

android:id  This is used to give a unique id to the layout. 
app:layout_constraintBottom_toBottomOf This is used to constrain the view with respect to the bottom position.
app:layout_constraintLeft_toLeftOf This attribute is used to constrain the view with respect to the left position.
app:layout_constraintRight_toRightOf This attribute is used to constrain the view with respect to the right position. 
app:layout_constraintTop_toTopOf This attribute is used to constrain the view with respect to the top position.

Advantages of using ConstraintLayout in Android

Disadvantages of using ConstraintLayout 

How ConstraintLayout differs from Linear Layout? 

How ConstraintLayout differs from RelativeLayout? 

How Constraint Layout differs from Grid Layout? 

Step by Step Implementation for adding Constraint Layout in Android 

Step 1: Create a New Project



To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Adding dependency for using Constraint Layout in Android



Navigate to the app > Gradle scripts > build.gradle file and add the below dependency to it in the dependencies section.

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

Now sync your project and we will move towards working with activity_main.xml. 

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Geeks for Geeks"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>

As we are working only with layout files so we don’t have to add any code in java or Kotlin file for MainActivity. After adding this code now we have to run the app to see the output of the app. 

Output:


Article Tags :