12

Fullscreen dialogs in material design should have the confirmation and dismissive actions on the actionbar/toolbar.

Material design fullscreen dialog

My question, how can i do this?

To show the dialog:

getFragmentManager().beginTransaction()
    .add(R.id.container, new MyDialogFragment())
    .addToBackStack(null).commit();

My dialog fragment:

public class MyDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.fragment_layout, container, false);
    }
}
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32
  • 1
    What does your layout file look like? I'm having trouble creating a full screen dialog and it seems you've managed that. – Jon Jun 23 '15 at 10:03
  • Can you elaborate on what difficulty you're experiencing? The layout itself should be as simple or complex is you want it to be, I assume you've done layouts for activities and fragments. The only visual difference for a full screen dialog are the areas I've highlighted in red. – SunnySydeUp Jun 23 '15 at 10:13
  • I think this question (http://stackoverflow.com/questions/30684312/android-full-screen-dialog-callback-issue) sums it up well. By default the dialog isn't full screen. – Jon Jun 23 '15 at 10:16
  • For a full screen dialog, you should treat the dialog as a fragment. MyDialogDragment fragment = new MyDialogFragment(); getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).addToBackStack(null).commit(); The android docs has a good example, scroll to the Dialog Fullscreen section: http://developer.android.com/guide/topics/ui/dialogs.html – SunnySydeUp Jun 23 '15 at 10:29
  • I end up with a weird transparent overlay that is slightly off screen. I'll keep looking and if I can't solve it, create a new question. Thanks anyway. – Jon Jun 23 '15 at 11:09
  • I guess I don't quite understand. Is this even a dialog anymore? Wouldn't you get the same result by extending/using a simple `Fragment` instead of `DialogFragment`? Is there any added benefit to using `DialogFragment`? – Dellkan Jun 23 '15 at 12:59
  • 1
    @Dellkan There are scenarios where you may want to display it as a dialog, such as on a bigger screen like a tablet, and then shown as a full screen fragment on smaller devices. A possible use case may be a login dialog. Since logins usually require simply a username and password, it's unnecessary to fill the entire tablet screen with the two EditText and a login button. So a dialog seems more appropriate. But on smaller devices, a dialog might be too small, so it might be better to show it as a full screen dialog. – SunnySydeUp Jun 23 '15 at 21:08
  • @SunnySydeUp Fair point, thanks for the clarification. – Dellkan Jun 24 '15 at 10:58

1 Answers1

20

Only two things need to be done:

  • Change the up icon
  • Add a menu to the fragment

Changing icon:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((ActionBarActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
}

Add Save menu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.save_menu, menu);
}

R.menu.save_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item
       android:id="@+id/save
       app:showAsAction="always|withText"
       android:title="@string/save"/>
</menu>
SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32