Search code examples
androidandroid-fragmentsnavigation-drawerandroid-alertdialogandroid-dialogfragment

Android Navigation Drawer & Dialog



is it possible to display an AlertDialog/DialogFragment and keep the Navigation Drawer open?
I have tried an Alert Dialog and a dialog fragment, but the drawer closes abruptly.

AlertDialog:

    new AlertDialog.Builder(this)
   .setTitle("Title")
   .setMessage("Message")
   .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
         // ...
      }
   })
   .setIcon(android.R.drawable.ic_dialog_alert)
   .show();

DialogFragment:

public class NewTermDialogFragment extends DialogFragment {

private EditText lectureName;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.dialog_new_term, null);

    lectureName = (EditText) view.findViewById(R.id.lectureText);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(view);
    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    return builder.create();
}
}

Call:

FragmentManager fragmentManager = getFragmentManager();

NewTermDialogFragment newTermDialogFragment = new NewTermDialogFragment();
newTermDialogFragment.setCancelable(false);
newTermDialogFragment.show(fragmentManager, "Dialog!");

Layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

        <FrameLayout 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=".RootActivity"
            tools:ignore="MergeRootFrame" />
    </LinearLayout>

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="de.uni.kassel.studentenapp.fragments.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />


</android.support.v4.widget.DrawerLayout>

Solution

  • Try showing it from NavigationDrawerFragment. It should work the way it supposed to be.

    Glad it helped.