12

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?

3
  • 1
    Does your first fragment set match_parent for width and height? Then you know what's going on...
    – ElDuderino
    Commented Mar 21, 2014 at 11:57
  • No it has wrap_content for layout_height attribute.
    – ferpar1988
    Commented Mar 21, 2014 at 12:01
  • 1
    @ferpar1988: as Mighter told you - layout container can host only one active fragment. therfore - you'll have to use to different layout containers if you want to show at the same time two different fragments
    – Tal Kanel
    Commented Mar 21, 2014 at 12:33

5 Answers 5

20

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();
2
  • I like this answer better because it's easier to implement this solution in a programmatic way, e.g. adding fragments numerically, and you can just use the indexes as tags.
    – DataGraham
    Commented Dec 29, 2014 at 3:52
  • 4
    I've having the same problem but this doesn't solve it. I had different tags for each fragment, but still it looks like they're overlapping each other. Any idea why?
    – Zip
    Commented Jan 13, 2017 at 19:41
9

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>
1
  • You can, but definitely don't have. I don't see why you would need to do this, AND it's not scalable.
    – jobbert
    Commented Jan 29, 2020 at 15:26
2

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.

2

solved it by adding this to the linearlayout:

android:orientation="vertical"
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.