This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints The

Viewed 67505

This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints.

The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX.) These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.

8 Answers

This may help someone. I am using Android Studio 3.1 and i faced same problem but i didn't find these options (Constraint Layout -> Infer Layout) as suggested in the above answer. Then after carefully checking i found "Infer Constraint" option above the layout window (refer to screenshot below in step 1)

Step 1: Click on "Infer Constraint". This may solve your problem, but if your widget still not visible (as in my case), then follow step 2. enter image description here

Step 2: Click on "AppTheme" and select "NoTitleBar" theme as selected in the screenshot enter image description here

This solved my problem.

Add the following piece of code to your code:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

From Android Studio v3 and up, Infer Constraint was removed from the dropdown.

Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button. Click on this button, this will automatically add some lines in the text field and the red line will be removed.

https://i.stack.imgur.com/77dfo.png

In my project this error was caused by android:layout_width="fill_parent" and android:layout_height="fill_parent" expressions. fill_parent constant is obsolete. Changing to to match_parent solved the problem:

<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

</ViewFlipper>

Android studio hint says:

This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints less... (Ctrl+F1) The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor.

To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.

Here is my fix (100% working):

1, Please drag and drop the images that you entered in folder "drawable". Note to keep the original name

2, Click the play(Green Triangle) button

3, It shows an error please click on your image and right click> Refactor> Rename> All place> Refactor and then run the application again> Restart

Note that the image name must not contain "-", "_"

When you run the app it will save the name to memory so just re-enter the image with your original name and then click Refactor> Rename> enter another name (must not contain any characters)> select All place(This will change the entire corrupted name to something that works better)> Refactor and rerun your application!

To position a SurfaceView in the center of the ConstraintLayout, using the holder setFixedSize, change layout_width to "wrap_content".

// setting holder
holder?.setFixedSize(640, 480)

XML

<androidx.constraintlayout.widget.ConstraintLayout
    android:id = "@+id/player_view"
    android:gravity = "center"
    android:layout_width = "0dp"
    android:layout_height = "0dp"
    app:layout_constraintTop_toTopOf = "parent"
    app: layout_constraintBottom_toBottomOf = "parent"
    app:layout_constraintEnd_toEndOf = "parent"
    app:layout_constraintStart_toStartOf = "parent">

        <SurfaceView
            android:id = "@+id/surfaceView"
            android:gravity = "center"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"

            app:layout_constraintTop_toTopOf = "parent"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>
Related