stack Learner
stack Learner

Reputation: 1348

How to dynamically include a merge layout?

I have a layout main.xml

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint1”
      android:layout_width="match_parent"
        android:layout_height="wrap_content"
     android:paddingBottom="@dimen/dimen_5"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        />


    <TextView
    android:id="@+id/textview1”
            android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="12dp"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:textSize="@dimen/ms_text_size"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
    <LinearLayout
    android:id="@+id/linearlayout1”
        app:layout_constraintTop_toBottomOf="@+id/textview1"
        app:layout_constraintStart_toStartOf="parent"
        android:orientation="vertical"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

Now in this xml for linearLayout i need to include another layout merge.xml dynamically.

<?xml version="1.0" encoding="utf-8"?>
        <merge xmlns:android="http://schemas.android.com/apk/res/android"
           >
            <TextView
                android:id="@+id/text1”

                android:textStyle="bold"
                android:textSize="@dimen/txt_size_14"
                android:paddingStart="@dimen/dimen_14dp"
                android:paddingEnd="@dimen/dimen_14dp"
                android:textColor="@color/grey"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/text2”

                android:paddingStart="@dimen/dimen_14dp"
                android:paddingEnd="@dimen/dimen_14dp"
                android:layout_marginTop="@dimen/dimen_2"
                android:textSize="@dimen/txt_size_17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </merge>

Code :

LinearLayout linearLayout = rootView.findViewById(R.id.linearLayout1);
View view = layoutInflater.inflate(R.layout.merge, null, false );
linearLayout.addView(view);

It is giving error.

android.view.InflateException: Binary XML file line #2: can be used only with a valid ViewGroup root and attachToRoot=true

Upvotes: 1

Views: 1980

Answers (3)

Pawel
Pawel

Reputation: 17288

Your error explains what you have to do. Replace this (both lines):

 View view = layoutInflater.inflate(R.layout.merge, null, false); 
 linearLayout.addView(view);

with this (one line):

layoutInflater.inflate(R.layout.merge, linearLayout, true);

So layoutInflater can properly resolve layout parameters.

Upvotes: 3

Wini
Wini

Reputation: 1996

try this -->

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

Upvotes: 0

Bruno
Bruno

Reputation: 4007

<merge> is not something you use programmatically because it's not a View. It's a compile-time attribute to indicate to merge the given layouts into any other layout that includes them.

Merge documentation: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

Upvotes: 1

Related Questions