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”

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
       ~~~~~~

Solution :

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

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

into ConstraintLayout as below

<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>

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

<?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>

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,

<?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>

Leave a Comment