DMC
DMC

Reputation: 1194

How to create multiple fragments programmatically?

I am trying to show multiple fragments on the one screen by creating them programmatically. I am able to do this no problem by including each fragment into the activities layout file. However when I try and do it programmatically Im confused.This is what I have so far for two fragments on a screen.

Main Class:

public class MainActivity extends FragmentActivity {

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

        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.imOne, one, "fragmentone");
        ft.add(R.id.imTwo, two, "fragmenttwo");
        ft.add(R.id.imThree, three, "fragmenthree");
        ft.commit();

    }
}

Fragment One:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_one, container, false);
        return view;
    }

}

Fragment Two:

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_two, container, false);
        return view;
    }

}

The layout files for my two fragment classes just contain a simple TextView. And then the activity_main.xml layout file for my main class:

    <?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="horizontal" >

    <FrameLayout
        android:id="@+id/imOne"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imTwo"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imThree"
        android:name="com.david.twofragexample.FragmentThree"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

When I try and do it programatically I get confused as to what should be in my activity_main.xml file. So from the Android Developer Guide(http://developer.android.com/guide/components/fragments.html) it says to use

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

So when I add this code to my onCreate method what changes do I have to make to my activity_main.xml file to do this programmatically?I don't see where R.id.fragment_container comes from. How can I determine where on the screen my new fragment will go?Thanks

EDIT: I am developing this on a tablet and not a phone.This has now been solved and the above code will add three fragments programmatically.

Upvotes: 4

Views: 14994

Answers (3)

Stephen Liu
Stephen Liu

Reputation: 141

As I don't have higher enough reps to comment yet, I will just leave the information here -- the way Rafael T suggested does work nicely, although I also had my doubt at the first place.

1) Define your xml like this:

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

2) Add each fragment into the same slot:

View box = v.findViewById(R.id.container);
FragmentTransaction ft = getFragmentManager().beginTransaction();
for (String catalog : mCatalogs) {
    Fragment fragment = HighlightedProductFragment.newInstance(catalog);
    ft.add(R.id.container, fragment, catalog);
}
ft.commit();

Thanks for the solution for this tricky requirement.

Upvotes: 2

accordionfolder
accordionfolder

Reputation: 574

<?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="horizontal" >

    <FrameLayout
        android:id="@+id/list"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/viewer"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

In your Activity:

FragmentOne one = new FragmentOne();
FragmentTwo two = new FragmentTwo();

fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.list, one, "fragmentone").commit();
fm.beginTransaction().replace(R.id.viewer, two, "fragmenttwo").commit();

I suggest you study this:

http://developer.android.com/reference/android/app/Fragment.html

and fully understand the fragment lifecycle, etc. The way I show is quick and dirty, but not neccesarilly best.

Upvotes: 3

Rafael T
Rafael T

Reputation: 15679

Try it like this:

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

as your activity_main.xml

You asked yourself the right question: where is the id fragment_container, and the answer was: nowhere. >ou also set your layout to horizontal, which makes it very likely, that added Views will be kind of 'out of screen'

Upvotes: 0

Related Questions