Constraint Layout gives error in default template in Android Studio 2.3
Asked Answered
S

7

6

I've updated Android Studio 2.3, after that I am getting default ConstrainLayout as template xml.

But in that I have RelativeLayout as Child Layout and I getting following warning.

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 design time 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.

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.pratikbutani.demoapp.MainActivity"
        tools:showIn="@layout/activity_main">

        <RelativeLayout 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:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:showIn="@layout/activity_main"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" />

        </RelativeLayout>

    </android.support.constraint.ConstraintLayout>
</layout>

Getting warning on RelativeLayout, What should I do?

Solitta answered 30/3, 2017 at 12:52 Comment(1)
For better layout performance, you should not embed a relative layout inside of a Constraint Layout. ConstraintLayout in itself does everything that Relative Layout does. You can add the RecyclerView directly under the Constraint Layout.Deprecatory
G
9

Go to Design, right-click on relative layout and select Constraint layout-->Infer Constraintsenter image description here

Gametangium answered 30/3, 2017 at 13:26 Comment(0)
O
3

Drag the object to connect to the walls...Create connections between walls and object

Look at the image example

enter image description here

Outrelief answered 21/12, 2017 at 18:54 Comment(1)
Exactly what I was looking for. Quick and simple! Wondering why is this not the accepted answer.Templas
L
1

Like that shown in picture make connection.In constraint layout we can place widgets anywhere but running on a app the widgets are not in same place where you placed during development in android studio.To fix this ,make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.Widgets has round like things in each and every side, drag that to adjust to fix its position.

https://i.sstatic.net/r8L8O.png

Lists answered 18/1, 2018 at 4:54 Comment(0)
P
0

This problem happend because you have use and your view don't have any constraint

tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"

It only effect (the effect here is your view will margin left 8dp and margin top 8dp) at design time (in Preview of Android Studio), at runtime it will not have any effect

To resolve this warning, you should add enough constraint for your Layout or simple remove both tools:layout_editor_absoluteY="8dp" and tools:layout_editor_absoluteX="8dp"

Professionalize answered 5/4, 2017 at 4:22 Comment(0)
M
0

this warning happened when your layout or widget has no constraint code but android view needs.For example, when u use android:layout_marginTop="12dp", you must add app:layout_constraintTop_toTopOf="", if not the warning appears.

Machinate answered 21/2, 2019 at 2:13 Comment(0)
B
0

Your relative layout is not constrained, constraint layout and relative layout are both standing alone. Go to the Design/Text tab to position correctly. You could also consider using just one of the two layout; Constraint layout works well as Relative layout

Bearskin answered 22/9, 2019 at 4:28 Comment(0)
B
0

First and foremost, since you're using a ConstraintLayout, you don't need to embed a child RelativeLayout inside.. I mean, that's just like driving two cars at the same time, it's redundant. You should either use only a RelativeLayout or only a ConstraintLayout.

However, if you still insist on this implementation, here's where the actual problem lies:

You used the tools:layout_editor_absoluteX and tools:layout_editor_absoluteY elements in your RelativeLayout and your RelativeLayout was not constrained. Due to this, the points of reference for absoluteX and absoluteY could not be determined at runtime. To fix this, simply constrain the RelativeLayout to the ConstraintLayout vertically and horizontally by adding these attributes to your RelativeLayout:

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

This way, your RelativeLayout would be constrained both verically (top and bottom) and horizontally (start and end).

I hope this helps. Merry coding!

Bedtime answered 22/9, 2019 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.