1

I could only find one thread pertaining to this and the answers didn't help fix the error. I am using a fresh install of Android Studio on a new laptop if that matters (maybe I need to install something that isn't currently there).

Error: This view is not constrained, it only has design time positions, so it will jump to (0,0) unless you add constraints

Edit2: It seems the new version of Android Studio doesn't use relative layout? I've tried manually changing the code as per this thread (How to switch from the default ConstraintLayout to RelativeLayout in Android Studio 2.3.3). Still not working.

Edit: Here is an example of the code from a previous project (as you can see, this is different from my actual current code):

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.squirreloverlord.ccarringtonphonephotoprint.MainActivity">

Below is my code for the main activity that is giving me the above error x4 (button/textView x2/radiogroup).

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mercenaryferret.ccarrington1_currency.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_title"
    android:textSize="24sp"
    android:textStyle="bold|italic"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.032" />

<EditText
    android:id="@+id/editText_usd"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/us_label"
    android:inputType="numberDecimal"
    android:textAlignment="center"
    android:textSize="12sp"
    tools:layout_editor_absoluteX="17dp"
    tools:layout_editor_absoluteY="69dp"
     />

<RadioGroup
    android:id="@+id/radioGrp"
    android:layout_width="213dp"
    android:layout_height="100dp"
    tools:layout_editor_absoluteX="86dp"
    tools:layout_editor_absoluteY="122dp"
    >

    <RadioButton
        android:id="@+id/radioButtonEuro"
        android:layout_width="98dp"
        android:layout_height="wrap_content"
        android:text="@string/euro_label"
        tools:layout_editor_absoluteX="110dp"
        tools:layout_editor_absoluteY="203dp" />

    <RadioButton
        android:id="@+id/radioButtonCanada"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/canada_label" />

    <RadioButton
        android:id="@+id/radioButtonMexico"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/mex_label" />
</RadioGroup>

<Button
    android:id="@+id/buttonConvert"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:text="@string/convert_label"
    android:textAlignment="center"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="248dp" />

<TextView
    android:id="@+id/textViewResults"
    android:layout_width="316dp"
    android:layout_height="31dp"
    tools:layout_editor_absoluteX="34dp"
    tools:layout_editor_absoluteY="354dp"
     />

Thank you in advance.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Charles Carrington
  • 101
  • 1
  • 2
  • 10
  • Possible duplicate of [This view is not constrained](https://stackoverflow.com/questions/37817537/this-view-is-not-constrained) – Zoe May 08 '19 at 14:18

3 Answers3

2

First of all at component tree there is ConstraintLayout left click on it and popup list opens then follow this :-

  1. Constraint Layout >> clear all constraints then
  2. Constraint Layout >> Add Infer Constraints
Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
1

You need to add some missing constraints, here is how to do that.

  1. Go to the Design View
  2. Right click on your Widget
  3. Click "Constraint Layout"
  4. Click "Infer Constraint"

If above does not work, try below.

  1. Go to the Design View
  2. Right click on your Widget
  3. Clear All Constraints
  4. Click "Constraint Layout"
  5. Click "Infer Constraint"
Atul Chavan
  • 1,754
  • 15
  • 12
  • In my experience, the behavior of the "Clear All Constraints" button is random. It most definitely does not clear all constraints from the layout consistently. – James Moore Jun 15 '19 at 17:43
1

When you are using ConstraintLayout you must constraint your views both vertically and horizontally or else they may jump to the corner of your screen at run time.

After you constraint, your views as I said it will be placed according to your constraint and be relative to your screen - and your views won't jump in runtime.

Example to this layout using constraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:text="app_title"
    android:textSize="24sp"
    android:textStyle="bold|italic"
    app:layout_constraintBottom_toTopOf="@+id/editText_usd"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.032" />

<EditText
    android:id="@+id/editText_usd"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:hint="us_label"
    android:inputType="numberDecimal"
    android:textAlignment="center"
    android:textSize="12sp"
    app:layout_constraintBottom_toTopOf="@+id/radioGrp"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toBottomOf="@+id/textView8" />

<RadioGroup
    android:id="@+id/radioGrp"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toTopOf="@+id/buttonConvert"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toBottomOf="@+id/editText_usd">

    <RadioButton
        android:id="@+id/radioButtonEuro"
        android:layout_width="98dp"
        android:layout_height="wrap_content"
        android:text="euro_label" />

    <RadioButton
        android:id="@+id/radioButtonCanada"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="canada_label" />

    <RadioButton
        android:id="@+id/radioButtonMexico"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="mex_label" />
</RadioGroup>

<Button
    android:id="@+id/buttonConvert"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="convert_label"
    android:textAlignment="center"
    app:layout_constraintBottom_toTopOf="@+id/textViewResults"
    app:layout_constraintEnd_toEndOf="@+id/editText_usd"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/editText_usd"
    app:layout_constraintTop_toBottomOf="@+id/radioGrp" />

<TextView
    android:id="@+id/textViewResults"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="some text"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/buttonConvert"
    app:layout_constraintStart_toStartOf="@+id/buttonConvert"
    app:layout_constraintTop_toBottomOf="@+id/buttonConvert" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.05" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.95" />

</androidx.constraintlayout.widget.ConstraintLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53