15

I would like to add a fragment to a dialog (it can be either a DialogFragment or a regular Dialog). How do I do that?

Here's my DialogFragment:

public class MyDialogFragment extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyDialogFragment2 dialog = new MyDialogFragment2();
        View v = inflater.inflate(R.layout.news_articles, container, false);
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit();
        return v;
    }

}

Here's news_article.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Here's my main activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            MyDialogFragment dialog = new MyDialogFragment();
            dialog.show(getSupportFragmentManager(), "asdf");
        }
    });
}

But when I try it I get:

No view found for id 0x7f070002 for fragment MyDialogFragment2

I think it's because the FragmentManager of the Activity isn't the one I should be adding to, but I can't find the one of the DialogFragment, where is it?

Blo
  • 11,903
  • 5
  • 45
  • 99
Kalisky
  • 1,141
  • 1
  • 9
  • 18
  • 2
    `getChildFragmentManager().beginTransaction()....` – user Aug 18 '13 at 10:30
  • Thanks, but this works from API 17 only, isn't it so? – Kalisky Aug 18 '13 at 10:41
  • 1
    With the native fragments yes as they were introduced from 4.2. But you always have the option of the fragments from the support compatibility package which works with the same `getChildFragmentManager()` method. – user Aug 18 '13 at 10:44
  • Oh... couldn't find it because of an old v4 version... found this: http://stackoverflow.com/questions/15805574/android-support-v4-app-fragment-undefined-method-getchildfragmentmanager, Thanks! – Kalisky Aug 18 '13 at 11:05

2 Answers2

15

The answer (thanks to @Luksprog) is using the getChildFragmentManager instead of getActivity().getSupportFragmentManager

It wasn't available for me, cause I had to upgrade my support-v4 jar, as described here: android.support.v4.app.Fragment: undefined method getChildFragmentManager()

Community
  • 1
  • 1
Kalisky
  • 1,141
  • 1
  • 9
  • 18
11

Layout for Dialog - R.layout.view_with_plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.util.me.TestActivity"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details"/>
    <fragment
        android:layout_toRightOf="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        class="com.util.me.test.PlusOneFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

How to show the Dialog

public void showDialog(View vIew){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null);
    builder.setView(view)
            .setPositiveButton("OK", null)
            .setNegativeButton("Cancel", null);

    AlertDialog dialog = builder.create();
    dialog.show();
}

The fragment in class="com.util.me.test.PlusOneFragment" is just a PlusOneFragment generated by Android Studio.

Tinashe
  • 3,115
  • 3
  • 25
  • 23
  • 2
    Attention this may have some problem when you show the dialog more than once. Static inflating fragment will add the fragment to Dialog's ownerActivity's fragmentManager, when you dismiss the dialog, the fragment is still there. So next time you inflate, will throw exception. – sailing Jul 04 '19 at 01:50