Learn Android — Creating Layouts — 2

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
5 min readJun 7, 2018

--

Welcome to the Learn Android series. I’m going to share with you, weekly, why and how to start with Android and build your portfolio. I’ll share with you explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learned how to create layouts in XML using the LinearLayout as parent. In this article, we will again focus on creating layouts, but this time using the latest introduction — the ConstraintLayout.

Previous Article: Learn Android — Creating Layouts — 1

Constraint Layout uses the concept of constraints to align views. The main advantage of using this layout is that, it reduces the hierarchy tree for complex layouts as compared to for example, with Linear Layout, which has a lot of nested view groups and components.

To define a view’s position, we need to add at least two constraints — one vertical and one horizontal. Nowadays, the Constraint Layout library is already added when we create a new repository / project. But if you want to add one to an older project, you can proceed as follows —

  1. In your module-level build.gradle” file, include —

2. Add the library as a dependency in the same “build.gradle” file —

3. In the sync notification, click Sync Project with Gradle Files.

We can also convert an existing Linear Layout to Constraint Layout with a single click. To do that, we first need to open the layout file and then in the Component Tree, we just need to right-click on the LinearLayout parent and select Convert LinearLayout to ConstraintLayout option.

The main constraints for any view are —

  1. start — to — startOf : refers to the positioning of Constraint from current view’s left to the left of destination view whose ID we need to provide as a value.
  2. start— to — endOf : refers to the positioning of Constraint from current view’s left to the right of destination view whose ID we need to provide as a value.
  3. end— to — endOf: refers to the positioning of Constraint from current view’s right to the right of destination view whose ID we need to provide as a value.
  4. end— to — startOf : refers to the positioning of Constraint from current view’s right to the left of destination view whose ID we need to provide as a value.
  5. top — to — topOf : refers to the positioning of Constraint from current view’s top to the top of destination view whose ID we need to provide as a value.
  6. top — to — bottomOf : refers to the positioning of Constraint from current view’s top to the bottom of destination view whose ID we need to provide as a value.
  7. bottom — to — topOf : refers to the positioning of Constraint from current view’s bottom to the top of destination view whose ID we need to provide as a value.
  8. bottom — to — bottomOf : refers to the positioning of Constraint from current view’s bottom to the bottom of destination view whose ID we need to provide as a value.

We can also create constraints to “parent” view. After creating constraints, we see two types of lines — zigzag style and chain style line.

For example, if we want to center a view with respect to the parent, the constraints we will apply to the component are highlighted in bold —

Now, let us create the similar layout that we created in the previous article. Here is the snapshot —

Layout that we will create using Constraint Layout.

Notice, how the hierarchy is reduced to flat one compared to the Linear Layout hierarchy, which might even get deeper and deeper as the complexity of the layout increases.

Also, are you thinking why have I used different attributes “left and right” instead of “start and end” while centering an element? The reason is that “start and end” can be used universally, whereas, “left and right” changes meaning when we want to distribute app in different languages which support RightToLeft writing compared to the common convention, like Arabic.

Constraint Layout uses similar weighted attributes as compared to Linear Layout. But is eases the arrangement of the subsequent child views without the need to create nested views for responsive arrangement as in Linear Layout parent view group. This helps us achieve the same goal in a fewer lines of code and also reduces the memory load on the mobile device to render the hierarchy.

We can also create Constraint Layout views using the Design Tab. As we hover to the top, bottom, start or end of the views, a hollow circle is displayed which indicates that the constraint can be dragged and dropped to the destination view. This automatically generates the code for us. But it’s always better to use the Text tab and write our own code so that we can modify it on our own terms.

For more information about Constraint Layouts, visit the official Android Documentation website!

Next up: Learn Android — Adding Interaction

Stay tuned for weekly updates! Follow me and Parsed Inc. to never miss another update.

Also, let’s become friends on LinkedIn, GitHub, Twitter and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--