Pratik Butani
Pratik Butani

Reputation: 62421

Constraint Layout gives error in default template in Android Studio 2.3

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?

Upvotes: 6

Views: 26196

Answers (7)

Taslim Oseni
Taslim Oseni

Reputation: 6263

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!

Upvotes: 0

PRAISE ELISHA
PRAISE ELISHA

Reputation: 91

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

Upvotes: 0

user1452641
user1452641

Reputation: 9

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.

Upvotes: 0

Linh
Linh

Reputation: 60923

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"

Upvotes: 0

tamil arasan
tamil arasan

Reputation: 64

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

Upvotes: 1

Samir Alakbarov
Samir Alakbarov

Reputation: 1145

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

Look at the image example

enter image description here

Upvotes: 3

J. Shiv
J. Shiv

Reputation: 144

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

Upvotes: 9

Related Questions