How to set the title of DialogFragment?
Does overriding onCreateDialog
and setting the title directly on the Dialog
work? Like this:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}
You can use getDialog().setTitle("My Dialog Title")
Just like this:
public static class MyDialogFragment extends DialogFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");
View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}
DialogFragment could be represented as dialog and as Activity. Use code below that would work properly for both
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}
Jason's answer used to work for me, but now it needs the following additions to get the title to show.
Firstly, in your MyDialogFragment's onCreate()
method, add:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
Then, in your styles.xml file, add:
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
After hours of trying different things, this is the only one that has done the trick for me.
NB - You may need to change the Theme.AppCompat.Light.Dialog.Alert
to something else in order to match the style of your theme.