Whenever you add an Android View
into a ConstraintLayout
view group, you might see an error message given by Android Studio saying This view is not constrained
as shown below:
This view is not constrained.
It only has designtime positions, so it will jump to (0,0)
at runtime unless you add the constraints
This is because a ConstraintLayout
requires any child View
inside the layout to have at least one horizontal and one vertical constraint for the View
.
In the screenshot below, the Button
view added to the layout is causing the error.
You can click the image to make it bigger:
To resolve this error, you need to add constraints to the Button
view that fulfills the minimum requirement of the ConstraintLayout
view.
You can do this in two ways:
- Using the Infer Constraints option from the Design window’s toolbar
- Using the Constrain option from the
View
’s context menu
The Infer Constraints option is used to let Android Studio scan the layout and determine the most effective set of constraints for all views.
You can click the button from the Design window toolbar as shown below:
The option will affect all views that have no constrain in your current layout.
In older Android Studio versions (version 2 and below) the Infer Constraints option can be accessed on the context menu of your views.
Right-click on the View
that has the error message and select Infer Constraints as shown below:
Besides the Infer Constraints option, you can also use the Constrain option to add constraints to the View
object.
Add constraints with the Constrains option
The Constrain option allows you to manually add a constraint angle to your View
object.
You need to right-click the View
that has the error message as shown below:
There are four constrains you can add to the View
:
parent top
andparent bottom
adds a vertical constraint to theView
parent start
andparent end
adds a horizontal constraint to theView
You only need to add one of each angle to resolve the error.
In the screenshot below, parent start
and parent bottom
constraints are added to the Button
view:
With that, the error message should disappear from your View
object.
And that’s how you resolve This view is not constrained
error when developing Android applications. 👍