⚡Fixing ConstraintLayout & Guideline AssertionError Exception in Android
Tags: androiddev, android, constraintlayout, mobile
Published: 2019-07-04 08:24:19

---
title: ⚡Fixing ConstraintLayout & Guideline AssertionError Exception in Android
published: true
tags: androiddev,android,constraintlayout,mobile
canonical_url: https://wajahatkarim.com/2019/03/fixing-constraintlayout--guideline-assertionerror-exception-in-android--/
---

#### A quick tip to fix AssertionError Exception when updating ConstraintLayout from 1.0.2 to later versions.

> This article is part of [Today I Learned](https://medium.com/p/65c64e1dcb6) series and was originally posted at my [TIL Github Repository](https://github.com/wajahatkarim3/Today-I-Learned/) and my website at [wajahatkarim.com](http://wajahatkarim.com)

***

These days, I am working on an android app with very old code base. So, as I am adding new functionality and fixing bugs, I am improving the code base with latest APIs. So, in that regard I added [Constraint Layout](https://developer.android.com/training/constraint-layout) about 3 months ago. At that time, the latest version was 1.0.2. This helped me creating complex layouts in easy manner and allowed me utilize the flat view hierarchy to make things perform better.

About yesterday, I had a task which involved showing/hiding multiple views at same time. I tried to use [Groups of Constraint Layout](https://developer.android.com/reference/android/support/constraint/Group), so I updated it to the next stable version, which was 1.1.3 .

But as I launched the app, some screens with ConstraintLayout were working and in some the app crashed. When I checked the log, I got this.

![](https://cdn-images-1.medium.com/max/1024/1*nFmmABYHe3KwaXm4Hw0yeg.png)

The log simply doesn’t make any sense. The only sensible thing, which can help me fix this issue, I got out of this log is the line # 2 which shows the keyword Guideline . So, I had a gut feeling that this is somehow an issue of Guideline.

***

Doing a simple Google search assures me that there are many other developers facing this same issue and no one has real clue of why this is happening and how it can be fixed.

The [threads](https://stackoverflow.com/questions/51594162/constraint-layout-issue-java-lang-assertionerror-top) [on](https://stackoverflow.com/questions/51247514/java-lang-assertionerror-left) [StackOverflow](https://stackoverflow.com/questions/50188620/constraint-layout-lib-update-from-1-0-2-to-1-1-0-got-error-guideline-getanchor) about this issue confirmed one thing that issue is related to the Guideline . There are even issues submitted at the [Google Codelab’s Constraint Layout repository](https://github.com/googlecodelabs/constraint-layout) like [this](https://github.com/googlecodelabs/constraint-layout/issues/51) and [this](https://github.com/googlecodelabs/constraint-layout/issues/53). Unfortunately, these issues are still open and there’s not a single comment yet, let alone the solution. The developers are left on their own to fix this issue.

![](https://cdn-images-1.medium.com/max/1024/1*VfotlhR7yerfwKOQNAYzIw.png)<figcaption>Screenshot of <a href="https://github.com/googlecodelabs/constraint-layout/issues/53">Issue # 53</a> from <a href="https://github.com/googlecodelabs/constraint-layout/">Constraint Layout at Github</a></figcaption>

***

### 🔥 Hot and ⌚️Quick Fix to This Frustrating Problem 😃

There’s no specific solution to this problem. But there’s a reason why this happens. So, to fix this issue, we need to understand that reason and then we can solve this easily.

[A Guideline in ConstraintLayout](https://developer.android.com/reference/android/support/constraint/Guideline) is a virtual helper, which will not be visible in runtime on the device and will have a visibility of View.GONE . These are only used for layout and positioning purposes.

![](https://cdn-images-1.medium.com/max/423/0*XMq9-dAZ9LzhOkXO.png)<figcaption>Two buttons are positioned with respect to a percentage vertical guideline</figcaption>

According to the documentation from Google, a Guideline can be either horizontal or vertical:

- Vertical Guidelines have a width of zero and the height of their [ConstraintLayout](https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html) parent
- Horizontal Guidelines have a height of zero and the width of their [ConstraintLayout](https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html) parent

The main cause of this AssertionError exception is this very thing.

> You cannot constraint any other view vertically to any vertical guideline and same is with horizontal.

What this means is that if we have a vertical guideline, then we cannot write code like this:

![](https://cdn-images-1.medium.com/max/1024/1*6qV6khrb_BzLiGzGQwMmDA.png)

Check line 16 and 17. We have set the TextView top and bottom to Vertical guideline’s Top and Bottom. A vertical guideline’s height is the parent’s height and we cannot constraint any view to the vertical guideline’s top and bottom. Rather we should constraint to the left and right of the vertical guideline and top/bottom to the HORIZONTAL guideline. So, if we write something like app:layout\_constraintLeft\_toRightOf=”@+id/guideline” , then we won’t have this issue.

***

### ✅ The Solution — Finally 😃

My project had like 15+ layout files which had ConstraintLayout . The log message doesn’t help much. So, first open all those layout files which have Guideline used.

Now the layout which is causing this problem will show a red error circle on top-right side of the preview pane as shown in the image below.

![](https://cdn-images-1.medium.com/max/492/1*X21oUN-7qUx9dp_HcMJmQA.png)

When you click on it, you will see the Message panel on bottom and this will tell TOP / BOTTOM for the vertical guidelines and LEFT / RIGHT for the horizontal guidelines. When you click on the Details button, you will see a dialog with the exact same exception message like the image below.

![](https://cdn-images-1.medium.com/max/1024/1*m3rpoRLKnf1Fgt736-OkUA.png)

This red error circle and this dialog is a confirmation that you are in the layout file which is causing this exception and crashing the app. So, cross check the guideline and other views’ constraints and make sure that vertical guideline is constrained for left and right and horizontal guideline is constrained for top and bottom. That’s the only solution for this annoying problem.

Once you do that, this red circle will disappear. And you can run your app without any problems and crashes now.

***

**If you liked this article, you can read my following new articles below:**

- [⚡ Is Your Android Studio Always Slow? 🚀Here’s How to Speed Up Immediately.](https://dev.to/wajahatkarim/-is-your-android-studio-always-slow-heres-how-to-speed-up-immediately-4fi1)

**Wajahat Karim** is a graduate from [NUST, Islamabad](http://nust.edu.pk), an experienced mobile developer, an active open source contributor, and co-author of two books [Learning Android Intents](https://www.amazon.com/Learning-Android-Intents-Muhammad-Usama/dp/1783289635) and [Mastering Android Game Development with Unity](https://www.amazon.com/Mastering-Android-Game-Development-Unity/dp/1783550775/). In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on [his blog](https://wajahatkarim.com)and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on [Github Trending](https://github.com/trending). [His libraries](http://github.com/wajahatkarim3) have about 3000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and [Medium](https://medium.com/@wajahatkarim3) to get more updates about his work in Writing, Android and Open Source.

* * *