Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
<?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
–
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.
–
–
–
<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
–
–
–
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));
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.