New features in ConstraintLayout 1.1.x

Google I/O 17   announce a new release of ConstraintLayout. But, we didn’t get so much info about what’s new features, only a very superficial blog post. No docs, no examples.


Sometimes layout can change depending on the localization. Here is a really simple example:

Constraint Layout barrier

Here we have three TextViews: Home and Office on the left and Description on the right.

A Barrier is a virtual view to which we can constrain objects. The position of a Barrier is determined by the dimensions of multiple views. In the case of our example, we don’t know whether Home or Office will be wider, so we can create a Barrier based upon the widths of these two Views. Then we can constrain Description to the Barrier.

Creating Barriers

First, we create a vertical barrier.

Next, we need to set the direction for the Barrier. In our case we want to position the Barrierrelative to the End of either Home or Office depending on whichever is the larger, so we need to specify a direction of end:

The final step in defining the Barrier is to tell it which Views we need to position it relative to. For a Barrier we need to define multiple Views as referenced IDs.

change the constraint of Descriptionto be relative to the Barrier rather than Home

Barrieris the Barrier element itself. The app:barrierDirectionattribute determines the direction of the Barrier – in this case it will be positioned at the end of the referenced Views.

The list of referenced Views is a comma separated list of the IDs of other Views within the layout (without the @+id/ qualifier).


Groups, like the Barrier, are widgets with size 0. The group helps to apply some action to a set of widgets. For example, The control a visibility of a collection of widgets. When dealing with this scenario, the most common solution was to maintain yourself a  set of views inside the Activity and put all the views inside of it, controlling the visibility of the container. Now, you only need to add their ids to the Group, and the group will propagate the actions to all plugged views.


A place where you can move a widget.You can create a virtual view and set its content to another view within the Constraint Layout.

Constraint Layout Placeholder

Download this project from GitHub.

In this simple example, It’s grabbing whichever one you clicked on and wrapping the middle of the screen.The animation you’re seeing on the right is completely being generated by the code you see in the example.

Design a placeholder-base UI with a merge tag. You’ve defined in ConstraintLayout, make sure to use the parent tag If you want to see the UI while you’re doing it.It essentially gives you a fixed layout which you can then use in another file.So in this case, I’ve decided to have one placeholder that’s going to be my top image, a big image and then and second placeholder that’s going to be my action button.

In Main Layout I just declare the two UI elements and include the template.Essentially it produces a template that decides on how you do your layout.

Another great feature from Placeholder is to change its referenced id at runtime, allowing you to create dynamic layouts and even cool animations with considerably less effort than using other ways.

Percent Dimensions

In 1.1.x, we have a new value, percent, that allow us to set a widget to take the percentage of the available space.


So that’s it! At least what I found myself. Like it and share if this was useful to you.

Related Post

ConstraintLayout 1.1.0: Circular Positioning


Room Persistence Library

In the database the best practice to put your database access into certain interfaces.We want to get rid of the SQLite boilerplate free code.Room Write boilerplate code for you.You define the interfaces Room provide the implementation.

Compile time validation

The room is going to give you an error at compile time.So it goes out and verifies your query against the schema you have defined.

Data Changes

LiveData gets notified when the data changes.Because it Knows your query, it knows what things affect it.So it can let you know if that query changes.

This is the part where all these architecture components work well together.

Your ViewModel all you would write is the data, from the data is call this query and this all it will do.Whenever that data changes, your UI will get a new update.

Adding Components to your Project

open the build.gradle file of  project  and add the line as shown below:

Open the build.gradle file for your app and add the artifacts that you need as dependencies:

Create Entity

Represents a class that holds a database row. For each entity, a database table is created to hold the items. You must reference the entity class in the Database class. Each field of the entity is persisted in the database unless you annotate it with @Ignore.

room entity

When a class is annotated with @Entity and is referenced in the entities property of a @Database annotation, Room creates a database table for that entity in the database.

Nested objects

You’d like to express a POJO as a cohesive whole in your database logic.In these situations, you can use the @Embedded annotation to represent an object that you’d like to decompose into its subfields within a table. You can then query the embedded fields just as you would for other individual columns.

Our User class can include a field of type Address. To store the composed columns separately in the table, include an Address field in the User class that is annotated with @Embedded, as shown in the following code snippet:

Indices and uniqueness

Index certain fields in the database to speed up your queries. To add indices to an entity, include the indices property within the @Entity annotation.You can enforce this uniqueness property by setting the uniqueproperty of an @Index annotation to true.


Room allows you to define Foreign Key constraints between entities.

For example, if there’s another entity called Book, you can define its relationship to the User entity using the @ForeignKey annotation, as shown in the following code snippet:

You can tell SQLite to delete all books for a user if the corresponding instance of User is deleted by including onDelete=CASCADE in the @ForeignKey annotation.

Data Access Objects (DAO)

This component represents a class or interface as a Data Access Object (DAO). DAO are responsible for defining the methods that access the database. When generating the code at compile time, Room creates an implementation of this class.


Create a DAO method annotate with @Insert .Room generates an implementation that inserts all parameters into the database in a single transaction.

The following code snippet shows example queries:

It can return a long, which is the new rowId for the inserted item. If the parameter is an array or a collection, it should return long[] or List<Long> .


It uses a query that matches against the primary key of each entity.


Deletes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.

Delete and Update method can return an int value instead, indicating the number of rows updated in the database.


It allows you to perform read/write operations on a database. Each @Query method is verified at compile time.

The room also verifies the return value of the query such that if the name of the field in the returned object doesn’t match the corresponding column names in the query response.

Querying multiple tables(JOIN)

The room allows you to write join tables. Furthermore, if the response is an observable data type, such as Flowable or LiveData, Room watches all tables referenced in the query for invalidation.

The following code snippet shows how to perform a table join to consolidate information between a table containing users who are borrowing books.

Data type converters

You sometimes use a custom data type whose value you would like to store in the database in a single column. To add this kind of support for custom types, you provide a TypeConverter, which converts a custom class to and from a known type that Room can persist.

Create Database

The annotation defines the list of entities, and the class’s content defines the list of data access objects (DAOs) in the database. It is also the main access point for the underlying connection.

The annotated class should be an abstract class that extends RoomDatabase.

The class must contain an abstract method that has zero arguments and returns the class that is annotated with @Dao.

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long periods of time.

At runtime, you can acquire an instance of it by calling Room.databaseBuilder() or Room.inMemoryDatabaseBuilder().

Download this project from GitHub.


Related Post

Room: Database Relationships

Room database Migrating