Home » Android » android – adding multiple fragments programmatically

android – adding multiple fragments programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using Fragment transaction to add two fragments to an activity. But it happens that only the first Fragment is shown when the app is started. Here is the code:

MainActivity

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragOne firstButton = new FragOne();
    FragmentTwo secButton = new FragmentTwo();

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.add(R.id.frag_container, firstButton);
    transaction.add(R.id.frag_container, secButton);

    transaction.commit();
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/frag_container"
    android:layout_height="fill_parent" 
    android:orientation="horizontal">

</LinearLayout>

frag_one.xml and frag_two.xml are similar

<?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:orientation="vertical" >
   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button One" />
</LinearLayout>

So I am not sure what could be the problem …I saw many examples with adding one fragment.

How to&Answers:

I’m not sure, but it’s possible that both the fragment are actually added but because they are exactly the same and are located in a LinearLayout – one is hiding the other.

If i were you I would change the layout in the main activity to be a relative layout and add the fragment to two different place holder to check if this is the problem.

I haven’t actually ran the program so it could be something else entirely… good luck!

Answer:

I know its too late to answer but I have figured out the problem. Your frag_one.xml and frag_two.xml look like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button One" />
</LinearLayout>

Observe that LinearLayout has layout_height set to match_parent. Wouldn’t it occupy the whole screen?

Just make it wrap_content and it would work. Just solved my case which was identical to yours.

Answer:

because the height params of both fragment xml file is “match_parent”

<?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"  WRONG!!! need to be "wrap_content"*
    android:orientation="vertical" >
   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button One" />
</LinearLayout>

therefore your first fragment inflate on the whole height of his parent
and when the other fragments added they dont have place to be.

therefore setting null to the container param in onCreateView method is not the right way to solve the problem.

you just need to set
android:layout_height=”wrap_content”

Answer:

My problem was solved by making sure that the LinearLayout had it’s orientation set to vertical instead of the default horizontal.

Answer:

Yes you are right, the fragments are both added but the problem is that the fragment layouts are added one over the other …The problem was in the fragments code

View view = inflater.inflate(R.layout.frag_one, container, false);

changed to

View view = inflater.inflate(R.layout.frag_one, null);