Search code examples
androidandroid-layoutandroid-fragmentsandroid-linearlayout

Place Fragments programmatically in LinearLayout


I am trying to add multiple fragments to a LinearLayout.

Container code

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    inflater.inflate(R.layout.fragment_container, container, false);
    View rootView=inflater.inflate(R.layout.fragment_container, container, false);

    LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.LinLay);
    linearLayout.addView(MyFragment.newInstance("Params", "Random"),0);

    return rootView;
}

XML for Container

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nt.projet.ToDoList">

    <!-- TODO: Update blank fragment layout -->

    <LinearLayout
        android:id="@+id/LinLay"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:animateLayoutChanges="true"
        android:layout_gravity="center"
        android:nestedScrollingEnabled="true"
        android:showDividers="end"
        android:translationZ="5dp"></LinearLayout>
</FrameLayout>

Fragment Code

public static MyFragment newInstance(String param1, String param2) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            inflater.inflate(R.layout.fragment_high_priority_task, container, false);
            View rootView = inflater.inflate(R.layout.fragment_high_priority_task, container, false);
            return rootView;
        }

I will add some onClickListeners to buttons that I will create in the Fragment. I want to programmatically add an indefinite number of fragments to the layout. How do I go about doing this?


Solution

  • You can not addView() and then pass in a new instance of a fragment. This is wrong:

    linearLayout.addView(MyFragment.newInstance("Params", "Random"),0);
    

    And change in your newInstance(...) to:

    MyFragment fragment = new MyFragment();
    

    And then use the FragmentTransaction

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if (mCurrentFragment != null) {
        mCurrentFragment = NewFragment.newInstance("Your Argument");
    }
    ft.add(R.id.LinLay, mCurrentFragment,);
    ft.commit();
    

    If you are in a Activity and not FragmentAcitivity you need to use getFragmentManager() instead of getSupportFragmentManager()