How to set the title of DialogFragment? How to set the title of DialogFragment? android android

How to set the title of DialogFragment?


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;    }    // ...}


Does overriding onCreateDialog and setting the title directly on the Dialog work? Like this:

@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {    Dialog dialog = super.onCreateDialog(savedInstanceState);    dialog.setTitle("My Title");    return dialog;}


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.