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:
dimens.xml
?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).
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.
Most of what can be achieved in LinearLayout and RelativeLayout can be done in 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.
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.
- Is there a way to tell Android Studio to not generate these?
No.
- 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.
- Would these absolutes cause some layout problems in other devices?
No, they are only used by the tools.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With