nilsi
nilsi

Reputation: 10761

Include layout file into constraint layout without affecting performance or Android Studio previews

I have a huge layout file with one flat constraint layout within.

I have android.support.constraint.Group elements that are identical. I want to move these to a separate file and then include them like <include layout="@layout/selection_group"/>

The problem I'm facing is that the group that I have in the file selection_group.xml is not previewed correctly in Android studio. Is there a way to make Android Studio preview this directly in the file or include them in a different way?

selection_group.xml

<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.Group
        android:id="@+id/top_bar_config_one_background_group"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/top_bar_background_with_border_fx"
        android:clickable="true"
        app:constraint_referenced_ids="top_bar_config_one,top_bar_tooth_one"
        app:layout_constraintBottom_toBottomOf="@+id/top_bar_container_background"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two"
        app:layout_constraintTop_toTopOf="@+id/top_bar_container_background" />

    <ImageView
        android:id="@+id/top_bar_config_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:scaleType="center"
        android:src="@drawable/ic_height_over_sea_100x26"
        app:layout_constraintBottom_toTopOf="@+id/top_bar_tooth_one"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two"
        app:layout_constraintTop_toTopOf="@+id/top_bar_config_one_background_group" />

    <ImageView
        android:id="@+id/top_bar_tooth_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/ic_tooth_auto_40x40"
        app:layout_constraintBottom_toBottomOf="@+id/top_bar_config_one_background_group"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two" />
    <?xml version="1.0" encoding="utf-8"?>
</layout>

I don't want to nest multiple constraint layouts together. I think the root <layout> view in selection_group.xml is optimized and does not affect the performance? My goal is to reduce redundant code and not affect performance

Upvotes: 0

Views: 361

Answers (2)

Radoslav Gospodinov
Radoslav Gospodinov

Reputation: 46

In order to make that layout file ready to be included you need to replace <layout> with <merge>

In case you need to use DataBinding into your layout you need to have <layout first and than <merge> tags on top of the layout file

I order to have proper preview in your inner layout which is going to be included - add on top level into merge tag:

tools:parentTag="ContraintLayout"

Something like this:

<merge 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"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

Upvotes: 1

Cheticamp
Cheticamp

Reputation: 62831

Try added a merge tag to the included layout:

<layout>
    <merge>
    <group>
    ... etc. ...
    </merge>
</layout>

Upvotes: 1

Related Questions