Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ConstraintLayout generates absolute values

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?
like image 229
Ankur Aggarwal Avatar asked May 29 '16 02:05

Ankur Aggarwal


People also ask

What is the use of ConstraintLayout in Android?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Why do we prefer constraint ConstraintLayout in Android?

Constraint Layout simplifies creating complex layouts in Android by making it possible to build most of your UI using only the visual editor in Android Studio. Constraint Layout comes with some powerful tools with the help of which you can define complex layouts without having deep nesting.

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.


2 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.

like image 188
ianhanniballake Avatar answered Sep 30 '22 17:09

ianhanniballake


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> 
like image 30
Jonah H. Avatar answered Sep 30 '22 19:09

Jonah H.