Getting started with ConstraintLayout in Kotlin - Part 5: building a simple app using ConstraintLayout

Introduction

In the previous parts of the series, we looked into ConstraintLayout and how you can use them to develop Android applications.

In this tutorial, we will use what we learned to create a sample application using ConstraintLayout. It is expected that you have completed the previous four parts of the series. We will replicate the design below using ConstraintLayout.

constraintLayout-5-demo

Prerequisites

To follow along in this part of the series you must have:

  • Completed the previous parts of the series.
  • Android Studio installed on your machine. You can check here for the latest stable versions available. A minimum version of 3.0 is recommended.
  • A basic knowledge on Android development and the ability to navigate through the Android Studio IDE.

Building our app layout

To get started, create your Android project in Android Studio. In the wizard, enter your project name, let’s name it ConstraintLayoutSample. Enter your package name for the application and select a minimum SDK of 19. Choose an Empty Activity template and leave the name as MainActivity. After this, Android studio will build your project and prepare it for you.

During the course of building this application, we will use a circular image view and we need a library for this.

Open your app build.gradle file and add this dependency:

1implementation 'de.hdodenhof:circleimageview:2.2.0'
2    implementation 'com.android.support:design:28.0.0-rc01'

Go ahead and sync your files after adding it.

We will implement the design in the MainActivity layout file. Open the activity_main.xml and replace it with this:

1<!-- File: ./app/src/main/res/layout/activity_main.xml -->
2    <?xml version="1.0" encoding="utf-8"?>
3    <android.support.constraint.ConstraintLayout 
4        xmlns:android="http://schemas.android.com/apk/res/android"
5        xmlns:app="http://schemas.android.com/apk/res-auto"
6        xmlns:tools="http://schemas.android.com/tools"
7        android:layout_width="match_parent"
8        android:layout_height="match_parent"
9        tools:context=".MainActivity">
10    
11        [...]
12    
13    </android.support.constraint.ConstraintLayout>

Above, we added the opening and closing tags of the ConstraintLayout. Within it, we will place our other views.

The first element we will add is the image of the artist. The image is included in our repository in the drawable folder as avicii.jpg. Paste this snippet inside the ConstraintLayout tag replacing any previous default views that may have existed there:

1<ImageView
2        android:id="@+id/avinciiImage"
3        android:layout_width="wrap_content"
4        android:layout_height="260dp"
5        android:scaleType="fitXY"
6        app:layout_constraintEnd_toEndOf="parent"
7        app:layout_constraintStart_toStartOf="parent"
8        app:layout_constraintTop_toTopOf="parent"
9        app:srcCompat="@drawable/avicii" />

As seen above, the image is constrained to the top of the layout, the start, and the end. The image has an height of 260dp and a scaleType of fitXY, which means it stretches the image to fit the size of the ImageView.

Next, we will add a View, which we can customise to give the image an orange overlay. In the same file, paste the following code:

1<View
2        android:id="@+id/view"
3        android:layout_width="0dp"
4        android:layout_height="260dp"
5        android:alpha="0.7"
6        android:background="#FF602D"
7        app:layout_constraintEnd_toEndOf="parent"
8        app:layout_constraintStart_toStartOf="parent"
9        app:layout_constraintTop_toTopOf="@id/avinciiImage" />

The view has the same height as the image and similar constraints. It has an orange background whose opacity is reduced slightly using the alpha attribute. This view is constrained to the top of the image.

Notice, from the design, that the elements on the left side of the layout all start from the same point, so, instead of applying margins to each, we will use a guideline.

Add this snippet to your layout:

1<android.support.constraint.Guideline
2        android:id="@+id/guideline"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:orientation="vertical"
6        app:layout_constraintGuide_begin="20dp" />

Above, we have a starting guideline of 20dp. Since all the elements on the right end at the same point also, we will apply a guideline for that too.

To do this, add the following to your layout XML:

1<android.support.constraint.Guideline
2        android:id="@+id/guideline2"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:orientation="vertical"
6        app:layout_constraintGuide_end="20dp" />

Finally, let’s add a horizontal guideline to the layout. To do this, add the following code to the XML layout file:

1<android.support.constraint.Guideline
2        android:id="@+id/guideline4"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:orientation="horizontal"
6        app:layout_constraintGuide_begin="20dp" />

At this point, your design should look like this:

constraintLayout-5-stage-1

Let’s start adding other views.

Switch back to the text view by clicking Text and then add the following ImageView after the guideline we added above:

1<ImageView
2        android:id="@+id/imageView5"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        app:layout_constraintStart_toStartOf="@+id/guideline"
6        app:layout_constraintTop_toTopOf="@+id/guideline4"
7        app:srcCompat="@drawable/ic_close" />

Above, we used a vector drawable file and we referenced it using app:srcCompat. The image is constrained to the top guideline and the left guideline.

Next, let’s create the vector drawable file we referenced above. Click File > New > Vector Asset. In the vector modal, set the name to ic_close, and click the Clip Art button then search for the close icon. Next, set the color of the icon to #ffffff then click Next > Finish.

constraintLayout-5-config-asset

Next, let’s add the TextView to hold the name of the artist:

1<TextView
2        android:id="@+id/textView"
3        android:layout_width="0dp"
4        android:layout_height="wrap_content"
5        android:text="Avincii"
6        android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
7        android:textColor="#FFF"
8        android:textSize="32sp"
9        app:layout_constraintBottom_toBottomOf="@+id/avinciiImage"
10        app:layout_constraintStart_toStartOf="@+id/guideline"
11        app:layout_constraintTop_toTopOf="@+id/guideline4" />

In the design, this text is close to the center between the top guideline and the bottom of the image. So we added a top and bottom constraint to the view.

Next, let’s add the smaller text under the artist name. Add this snippet to your layout:

1<TextView
2        android:id="@+id/textView2"
3        android:layout_width="0dp"
4        android:layout_height="wrap_content"
5        android:layout_marginTop="5dp"
6        android:text="Trance/ House"
7        android:textColor="#FFF"
8        app:layout_constraintStart_toStartOf="@+id/guideline"
9        app:layout_constraintTop_toBottomOf="@+id/textView" />

For the START RADIO text add this snippet:

1<TextView
2        android:id="@+id/textView3"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:text="START RADIO"
6        android:textColor="#FFF"
7        android:textSize="18sp"
8        app:layout_constraintBottom_toBottomOf="@+id/avinciiImage"
9        app:layout_constraintStart_toStartOf="@+id/guideline"
10        app:layout_constraintTop_toBottomOf="@+id/textView2" />

This view is constraint to the bottom of the image, the starting left guideline and the bottom of the TextView added before this.

After that, add the +FOLLOW text like this:

1<TextView
2        android:id="@+id/textView4"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:layout_marginStart="24dp"
6        android:text="+FOLLOW"
7        android:textColor="#FFF"
8        android:textSize="18sp"
9        app:layout_constraintStart_toEndOf="@+id/textView3"
10        app:layout_constraintTop_toTopOf="@+id/textView3" />

This TextView is constrained such that it starts at the end of the start radio text. It is also constrained to the top of the start radio TextView, which means, its vertically aligned with it.

Next, let’s add the Top tracks text and the play icon using chains. Add this to your layout:

1<TextView
2        android:id="@+id/textView5"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:layout_marginTop="20dp"
6        android:text="Top tracks"
7        android:textSize="16sp"
8        app:layout_constraintEnd_toStartOf="@+id/imageView6"
9        app:layout_constraintHorizontal_bias="0.5"
10        app:layout_constraintHorizontal_chainStyle="spread_inside"
11        app:layout_constraintStart_toEndOf="@+id/guideline"
12        app:layout_constraintStart_toStartOf="@+id/guideline"
13        app:layout_constraintTop_toBottomOf="@+id/view" />
14    
15    <ImageView
16        android:id="@+id/imageView6"
17        android:layout_width="wrap_content"
18        android:layout_height="wrap_content"
19        android:layout_marginEnd="8dp"
20        android:layout_marginRight="8dp"
21        android:layout_marginTop="8dp"
22        app:layout_constraintBottom_toBottomOf="@+id/textView5"
23        app:layout_constraintEnd_toStartOf="@+id/guideline2"
24        app:layout_constraintHorizontal_bias="0.5"
25        app:layout_constraintStart_toEndOf="@+id/textView5"
26        app:layout_constraintTop_toTopOf="@+id/textView5"
27        app:srcCompat="@drawable/ic_play_circle" />

These two views are chained using the spread_inside chain mode. This means, they are attached to the end of the chain, in this case, the guidelines. If we had more than two views, the views inside would be spread evenly.

Next, let’s create the vector drawable file we referenced above. Click File > New > Vector Asset. In the vector modal, set the name to ic_play_circle, and click the Clip Art button then search for the play circle outline icon. Next, set the color of the icon to #000 then click Next > Finish.

Next, we lets add more guidelines. Add this to your layout:

1<android.support.constraint.Guideline
2        android:id="@+id/guideline5"
3        android:layout_width="wrap_content"
4        android:layout_height="wrap_content"
5        android:orientation="vertical"
6        app:layout_constraintGuide_percent="0.2" />
7    
8    <android.support.constraint.Guideline
9        android:id="@+id/guideline6"
10        android:layout_width="wrap_content"
11        android:layout_height="wrap_content"
12        android:orientation="vertical"
13        app:layout_constraintGuide_percent="0.85" />

As we mentioned earlier, guidelines prevent repetitive margins. For the guidelines we created above, we opted to use percent.

Your layout should look like this now:

constraintLayout-5-stage-2

The first guideline is placed at the 20% of the layout and the second at 85%.

Next, let’s add an image for the first track as seen on the design we are implementing. We are using a circular image view. Add this to your snippet:

1<de.hdodenhof.circleimageview.CircleImageView 
2        xmlns:app="http://schemas.android.com/apk/res-auto"
3        android:id="@+id/profile_image"
4        android:layout_width="0dp"
5        android:layout_height="50dp"
6        android:layout_marginTop="8dp"
7        android:src="@drawable/album"
8        app:civ_border_color="#FF000000"
9        app:civ_border_width="2dp"
10        app:layout_constraintBottom_toBottomOf="@+id/textView7"
11        app:layout_constraintEnd_toStartOf="@+id/guideline5"
12        app:layout_constraintStart_toStartOf="@+id/guideline"
13        app:layout_constraintTop_toTopOf="@+id/textView6" />

The image above is constrained to the start guideline and it ends before the start of the 0.2 percent guideline. It is also constrained to the top tracks text.

Next, let’s add the name of the first track:

1<TextView
2        android:id="@+id/textView6"
3        android:layout_width="0dp"
4        android:layout_height="wrap_content"
5        android:layout_marginTop="20dp"
6        android:text="Wake me up"
7        android:textColor="#232323"
8        android:textSize="15sp"
9        android:textStyle="bold"
10        app:layout_constraintEnd_toStartOf="@+id/guideline6" 
11        android:layout_marginStart="10dp"
12        app:layout_constraintStart_toStartOf="@+id/guideline5"
13        app:layout_constraintTop_toBottomOf="@+id/textView5" />

This textview is constrained to the start of the 0.2 percent guideline and a start margin of 10dp was also added. It’s also constrained to end at the 0.85 percent guideline, and finally, it’s constrained to the bottom of top tracks text.

Next, let’s add a text under the song title:

1<TextView
2        android:id="@+id/textView7"
3        android:layout_width="0dp"
4        android:layout_height="wrap_content"
5        android:layout_marginTop="4dp"
6        android:text="True"
7        android:layout_marginStart="10dp"
8        app:layout_constraintEnd_toStartOf="@+id/guideline6"
9        app:layout_constraintStart_toStartOf="@+id/guideline5"
10        app:layout_constraintTop_toBottomOf="@+id/textView6" />

We then add an horizontal line as seen in the design:

1<View
2        android:id="@+id/view2"
3        android:layout_width="0dp"
4        android:layout_height="0.5dp"
5        android:layout_marginTop="4dp"
6        android:background="#232323"
7        app:layout_constraintEnd_toEndOf="parent"
8        app:layout_constraintStart_toStartOf="@+id/guideline5"
9        app:layout_constraintTop_toBottomOf="@+id/profile_image" />

Next we need to add options icon for the track as seen in the design. To do this, add the following code to the layout file:

1<ImageView
2        android:id="@+id/imageView7"
3        android:layout_width="0dp"
4        android:layout_height="0dp"
5        android:layout_marginEnd="8dp"
6        android:layout_marginStart="8dp"
7        app:layout_constraintBottom_toBottomOf="@+id/textView7"
8        app:layout_constraintEnd_toStartOf="@+id/guideline2"
9        app:layout_constraintStart_toStartOf="@+id/guideline6"
10        app:layout_constraintTop_toTopOf="@+id/textView6"
11        app:srcCompat="@drawable/ic_more_vert" />

This icon is aligned to the bottom of the True text. That means it ends on the same vertical position as the text. It also starts on the same vertical position as the title of the song - Wake me up.

Next, let’s create the vector drawable file we referenced above. Click File > New > Vector Asset. In the vector modal, set the name to ic_more_vert, and click the Clip Art button then search for the more vert icon. Next, set the color of the icon to #000 then click Next > Finish.

As seen in the design, we have two tracks. Since they have the same design, we just duplicated and made few constraint adjustments.

Add this to your layout:

1<de.hdodenhof.circleimageview.CircleImageView 
2        xmlns:app="http://schemas.android.com/apk/res-auto"
3        android:id="@+id/profile_image2"
4        android:layout_width="0dp"
5        android:layout_height="50dp"
6        android:layout_marginTop="8dp"
7        android:src="@drawable/album"
8        app:civ_border_color="#FF000000"
9        app:civ_border_width="2dp"
10        app:layout_constraintBottom_toBottomOf="@+id/textView9"
11        app:layout_constraintEnd_toStartOf="@+id/guideline5"
12        app:layout_constraintStart_toStartOf="@+id/guideline"
13        app:layout_constraintTop_toTopOf="@+id/textView8" />
14    
15    <TextView
16        android:id="@+id/textView8"
17        android:layout_width="0dp"
18        android:layout_height="wrap_content"
19        android:layout_marginTop="20dp"
20        android:text="Levels"
21        android:textColor="#232323"
22        android:textSize="15sp"
23        android:textStyle="bold"
24        android:layout_marginStart="10dp"
25        app:layout_constraintEnd_toStartOf="@+id/guideline6"
26        app:layout_constraintHorizontal_bias="0.0"
27        app:layout_constraintStart_toStartOf="@+id/guideline5"
28        app:layout_constraintTop_toBottomOf="@+id/view2" />
29    
30    <TextView
31        android:id="@+id/textView9"
32        android:layout_width="0dp"
33        android:layout_height="wrap_content"
34        android:layout_marginTop="4dp"
35        android:text="Stories"
36        android:layout_marginStart="10dp"
37        app:layout_constraintEnd_toStartOf="@+id/guideline6"
38        app:layout_constraintStart_toStartOf="@+id/guideline5"
39        app:layout_constraintTop_toBottomOf="@+id/textView8" />
40    
41    <View
42        android:id="@+id/view3"
43        android:layout_width="0dp"
44        android:layout_height="0.5dp"
45        android:layout_marginTop="4dp"
46        android:background="#232323"
47        app:layout_constraintEnd_toEndOf="parent"
48        app:layout_constraintStart_toStartOf="@+id/guideline5"
49        app:layout_constraintTop_toBottomOf="@+id/profile_image2" />
50    
51    <ImageView
52        android:id="@+id/imageView8"
53        android:layout_width="0dp"
54        android:layout_height="0dp"
55        android:layout_marginEnd="8dp"
56        android:layout_marginLeft="8dp"
57        android:layout_marginRight="8dp"
58        android:layout_marginStart="8dp"
59        app:layout_constraintBottom_toBottomOf="@+id/textView9"
60        app:layout_constraintEnd_toStartOf="@+id/guideline2"
61        app:layout_constraintStart_toStartOf="@+id/guideline6"
62        app:layout_constraintTop_toTopOf="@+id/textView8"
63        app:srcCompat="@drawable/ic_more_vert" />

Finally, let’s add the FloatingActionButton like so:

1<android.support.design.widget.FloatingActionButton
2      android:id="@+id/floatingActionButton"
3      android:layout_width="wrap_content"
4      android:layout_height="wrap_content"
5      android:layout_marginBottom="16dp"
6      android:clickable="true"
7      app:elevation="16dp"
8      app:backgroundTint="#FF602D"
9      app:layout_constraintBottom_toBottomOf="parent"
10      app:layout_constraintEnd_toStartOf="@+id/guideline2"
11      app:srcCompat="@drawable/ic_shuffle" />

Next, let’s create the vector drawable file we referenced above. Click File > New > Vector Asset. In the vector modal, set the name to ic_shuffle, and click the Clip Art button then search for the shuffle icon. Next, set the color of the icon to #fff then click Next > Finish.

You can remove the application toolbar by updating it in your styles.xml file:

1<resources>
2        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
3            <item name="windowActionBar">false</item>
4            <item name="windowNoTitle">true</item>
5        </style>
6    </resources>

Now, If you run your app, you should see this:

constraintLayout-5-demo

Conclusion

This ends our constraint layout part series. In this series, we have learned about Android’s most powerful layout system. In this part, we went through the process of building a layout from ground up using ConstraintLayout.

You can find the repository for this project here.