Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding multiple fragments programmatically

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.

like image 594
ArloGrant Avatar asked Jan 27 '13 15:01

ArloGrant


People also ask

How do you add multiple fragments to an activity?

Step 1 — Create a base activity The first step is to create a base activity which is gonna host the fragments which we need to display. In Android Studio, Right click on main source set folder, click on New -> Activity -> Empty Activity. Now you have created the base activity.

Can we add fragment in LinearLayout?

You can have multiple fragments in a LinearLayout .

Can fragments be used in multiple activities?

You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment. With this in mind, you should only provide a fragment with the logic necessary to manage its own UI.

How add and remove fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


2 Answers

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.

like image 106
Manish Kumar Sharma Avatar answered Nov 08 '22 22:11

Manish Kumar Sharma


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!

like image 43
Dror Fichman Avatar answered Nov 08 '22 22:11

Dror Fichman