Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar in DialogFragment

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

like image 853
Edu Barbas Avatar asked Dec 22 '14 18:12

Edu Barbas


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.


2 Answers

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

like image 157
mmlooloo Avatar answered Oct 18 '22 00:10

mmlooloo


Yes, DialogFragment has setHasOptions() function. Define the toolbar in the layout of your dialog and use it as if it is in an activity. A toolbar doesnt mind being in an activity or a fragment or a dialog fragment.......

Be sure that you use

setHasOptionsMenu(true) in onActivityCreated method....

Then, as usual override

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

and

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
like image 33
Hirak Chhatbar Avatar answered Oct 18 '22 00:10

Hirak Chhatbar