Home » Android » Android ConstraintLayout generates absolute values

Android ConstraintLayout generates absolute values

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have recently started learning the new ConstraintLayout in Android Studio 2.2 and noticed that when I add simplest of the views, the layout editor automatically generates some absolute coordinates. Here is a sample XML:

<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:id="@+id/activity_portfolio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abc.Activity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:text="@string/creator_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="246dp"
        tools:layout_editor_absoluteY="479dp"
        android:id="@+id/first_textview"
        app:layout_constraintRight_toRightOf="@+id/activity"
        android:layout_marginEnd="16dp"
        tools:layout_constraintRight_creator="0"
        app:layout_constraintBottom_toBottomOf="@+id/activity"
        android:layout_marginBottom="16dp"
        tools:layout_constraintBottom_creator="0" />
</android.support.constraint.ConstraintLayout>

Notice the absolutes like 81dp, 246dp, 479dp… I tried to manually delete these, but when I go back to the “Design” tab and come back to the “Text” tab, these regenerate. Now, I have three questions:

  1. Is there a way to tell Android Studio to not generate these?
  2. Should I manually place them in dimens.xml?
  3. Would these absolutes cause some layout problems in other devices?
How to&Answers:

You’ll note that all of the absolute values are in the tools namespace – this means they are not compiled into your app, nor used in anything but in the tools (and in this case, the visual editor). They are simply to ensure that switching from the Design to Text tab is always consistent, with the underlying files remaining stable.

  1. Is there a way to tell Android Studio to not generate these?

No.

  1. Should I manually place them in dimens.xml?

These are only useful for the tools and therefore should not be added to a separate dimens.xml file that would be included in your final APK.

  1. Would these absolutes cause some layout problems in other devices?

No, they are only used by the tools.

Answer:

I’m not sure your original question contains your entire layout, as it references a widget with an id of @+id/activity, so the issue might lie elsewhere in your layout.

Ensure that no widget that exists within a ConstraintLayout has a layout_width or layout_height of match_parent.

MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to “parent”.

Source

If you use match_parent, Android Studio will generate these absolute values, as well as replacing match_parent with an absolute dimension.

Based on the layout you posted, your TextView probably had a layout_width or layout_height of match_parent before Android Studio replaced it.

You should replace android:layout_width="match_parent" with

android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"

And android:layout_height="match_parent" with

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"

In your specific layout, you probably want something like this:

<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:id="@+id/activity_portfolio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abc.Activity">

    <TextView
        android:text="@string/creator_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/first_textview"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/activity"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>

Answer:

As a side notes to given answers, you can use the Magic Wand Icon in the toolbar menu above the design preview. Click on Infer Constraints button, this will automatically add some lines in the text field and the tools one will be converted to constrained.

Please see below picture :

enter image description here