Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout Problems using <include>

I am trying to figure out why this does not work. I am adding nothing but two <include> sections in a ConstraintLayout and the layout is not following any of the constraints that I set up. I am trying to begin migrating to the use of ConstraintLayout as my go-to layout, but things like this keep pushing me back to RelativeLayout and LinearLayout.

Here is the top level layout file (the ConstraintLayout), showing some of the constraints that are not working:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <include
        android:id="@+id/includeButton"
        layout="@layout/include_button_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <include
        android:id="@+id/includeText"
        layout="@layout/include_text_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout

Here is the first included layout (include_button_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/include_button_panel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch me!" />

</merge>

Here is the second included layout (include_text_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/include_text_panel_text"
        android:layout_width="wrap_content"
        android:background="#7986cb"
        android:layout_height="wrap_content"
        android:text="This is the text panel"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</merge>
like image 857
Mike Avatar asked Sep 19 '17 02:09

Mike


People also ask

Is ConstraintLayout better than RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).

Should you use ConstraintLayout?

Well, each layout has its own benefits but when it comes to complex, dynamic and responsive views you should always choose Constraint Layout. Constraint Layout was added to Android Studio 2.2 in 2016 and it became the default layout of Android Studio because of its simplicity and ease of creating complex layouts.

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?

It's not the right way. ConstraintLayout came to prevent the usage of nested layouts. Remove the relative layout and put a view on top(zindex) of your clickable zone.


2 Answers

Remove the <merge> tag from both of your included layouts.

When you specifiy attributes on an <include> tag, the system will apply those attributes to the root view of the included layout. The <merge> tag is a special element to allow you to have multiple views in your layout without having a root view. So all of your constraints get thrown out when your included layout uses the <merge> tag. Luckily, both of your included layouts only have one view inside the merge, so you can just remove the merge altogether.

like image 124
Ben P. Avatar answered Nov 02 '22 19:11

Ben P.


try this Remove the <merge> tag from both of your included layouts

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <include
        android:id="@+id/includeButton"
        layout="@layout/include_button_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
        android:id="@+id/includeText"
        layout="@layout/include_text_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
like image 41
AskNilesh Avatar answered Nov 02 '22 20:11

AskNilesh