Home » Android » android – Cannot add multiple fragments to LinearLayout

android – Cannot add multiple fragments to LinearLayout

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using a LinearLayout with a vertical orientation to list fragments. I add fragments to the container programmatically like this:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1);

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2);

ft.commit();

But it only shows the first fragment. Why?

How to&Answers:

You can have multiple fragments in a LinearLayout.

According to the documentation,

If you’re adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy

The problem with your code is that because you didn’t specify fragment tags, it defaulted to the container id. Since the container id is the same for both transactions, the 2nd transaction replaced the 1st fragment, rather than added it to the container separately.

To do what you want, use something like:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1, "fragment_one");

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2, "fragment_two");

ft.commit();

Answer:

I guess you have to define separe containers in your layout for each Fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_secondary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

     <FrameLayout
         android:id="@+id/content_primary"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="1" >
     </FrameLayout>

</LinearLayout>

Answer:

Had the same problem, but using

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.control_list, container, false);

I.E. using a xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/control_list">
</LinearLayout>

Solved my problem.
When creating the LinearList programmatically only the first fragment showed up for me too.

That is using an xml file for the layout you want to add the fragments to.

Answer:

solved it by adding this to the linearlayout:

android:orientation="vertical"

Answer:

I had this problem because the layouts that I was inflating had a height of “match_parent” instead of “wrap_content”. The LinearLayout and it’s container’s height is unchanged.

TestFragment.java

...
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.test_fragment_layout, container, false);
}
...

test_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- ... -->

</android.support.constraint.ConstraintLayout>