0

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

nilsi
  • 10,351
  • 10
  • 67
  • 79
  • Did you try to invalidate cache and restart android studio? You can check out this thread too. https://stackoverflow.com/questions/34499839/android-studio-does-not-show-layout-preview If this helps your cause. – Pronoy999 Jan 23 '20 at 09:22

2 Answers2

1

Try added a merge tag to the included layout:

<layout>
    <merge>
    <group>
    ... etc. ...
    </merge>
</layout>
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • Merge was exactly what I needed. Right-click refactor -> layout did this for me I figured. Didn't need the outer tags. – nilsi Jan 23 '20 at 14:24
1

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">