Solved: Error: This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints [MissingConstraints]

When we were trying to add a simple button into our already existing code of some other project, we got following error after we add a button code by following our another post “Android demo application with button click event”

[bash] app/src/main/res/layout/activity_main.xml:9: Error: This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints [MissingConstraints] <Button ~~~~~~ [/bash]

Solution :

The mistake we had made is we were trying to add button view,

[bash] <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="ClickButton" /> [/bash]

into ConstraintLayout as below

[bash] <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="ClickButton" /> </androidx.constraintlayout.widget.ConstraintLayout> [/bash]

So, to resolve this error, we changed the ConstraintLayout to RelativeLayout in app/src/main/res/layout/activity_main.xml.

[bash] <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="ClickButton" /> </RelativeLayout> [/bash]

OR if you can’t change the ConstraintLayout to RelativeLayout

the other solution is to change your Button parameters in app/src/main/res/layout/activity_main.xml as below,

[bash] <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:text="ClickButton" /> </androidx.constraintlayout.widget.ConstraintLayout> [/bash]

Leave a Comment