Constraint Layout Advantage and example in kotlin

Android Constraint Layout exampleConstraint Layout

The main advantage of ConstraintLayout is allows you to make large and complex layouts with a flat view hierarchy. No nested view groups like inside RelativeLayout or LinearLayout etc. You can make Responsive UI for android using ConstraintLayout and its more flexible compare to RelativeLayout.

Before start if your beginner then follow this tutorial – Build Your First Android App in Kotlin

Add ConstraintLayout to your project

Add the library as a dependency in the same build.gradle file: Then see in the toolbar or sync notification, click Sync Project with Gradle Files.

  • Every view must have at least two constraints: one horizontal and one vertical.
  • If a view has no constraints when you run your layout on a device, it is drawn at position [0,0] (the top-left corner).

Example 1 : Here is simple example of using ConstraintLayout

1.  For design support library your needed add dependency in build.gradle (Module: app)

2.  Then add following code activity_login.xml layout file

3. Add code in LoginActivity.kt

4. Check all dependency build.gradle (Module: app) should have

Run the project

Output Screen :

Android ConstraintLayout example

Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)



Coding in Kotlin

Advantages :

  • To make handling complex screen designs easier
  • To improve the performance of complex layouts
  • Drag and drop GUI builder

Bonus : for more about Constraint Layout  details check this tutorial : Differences between ConstraintLayout and RelativeLayout

Download source code Constraint Layout Advantage and example in kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.