Toolbar in DialogFragment

Edu Barbas picture Edu Barbas · Dec 22, 2014 · Viewed 12.6k times · Source

In our app, we have a few fragments that can be shown either as fullscreen fragments or as dialog fragments. These fragments inherit from DialogFragment and we make sure to instantiate them correctly depending the mode the app is executed in (either fullscreen or dialog).

We thought about adding some extra functionality to some of these dialog fragments after the latest changes in the Toolbar widget were introduced in the support library with Lollipop. The idea is to have the type of options menu we would normally have in an ordinary fragment (i.e. the options menu inflated after onCreateOptionsMenu is executed) present in our subclasses of DialogFragment ONLY when these are visualized as dialogs. In short: when the fragments are shown in fullscreen mode we inflate a traditional options menu, and when the fragments are shown as dialogs we would like to have the same options menu inflated but using the new toolbar widget in standalone mode.

I followed the steps from http://android-developers.blogspot.dk/2014/10/appcompat-v21-material-design-for-pre.html and I managed to "activate" the toolbar, but it seems the menu is not inflated - see attached screenshots (picture one fragment in fullscreen mode, and picture two in dialog mode).

Is it even possible to inflate an options menu with the new toolbar in a DialogFragment?

fullscreen modedialog mode

Answer

mmlooloo picture mmlooloo · Dec 22, 2014

Is it even possible to inflate an options menu with the new toolbar in a DialogFragment?

first of all your design is ok and toolbar is supposed to be used every where lets have a look at this from Chris Banes google engineer link:

// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(
        new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // Handle the menu item
                return true;
            }
});

// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);

and also android developer toolbar standalone sample:

http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html