166

This should be a simple task, but for some reason I can find a way to set the title of a DialogFragment. (I am setting the dialog contents using onCreateView overload).

The default style leaves a place for the title, but I can't find any method on the DialogFragment class to set it.

The title is somehow magically set when the onCreateDialog method is used to set the contents, so I wonder if this is by design, or there is a special trick to set it when using the onCreateView overload.

7 Answers 7

316

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;
    }
    // ...
}
7
  • 6
    Yes, indeed getDialog().setTitle() does the trick. After looking at the solution it does make a lot of sense to work that way but for some reason I expected the call to set the title to be on the DialogFragment class itself (same place where setStyle() and DialogFragment.STYLE_NO_TITLE are defined). Thank you very much for the solution.
    – StefanK
    Apr 8, 2011 at 11:53
  • 1
    What if I wanted to change the background of the title. Is that possible?
    – MinceMan
    Jan 22, 2012 at 2:55
  • 5
    Also note that you can't set the dialog title in the fragment's onCreate() method since the dialog hasn't been created yet :)
    – greg7gkb
    Jun 22, 2012 at 18:28
  • 2
    @Rob Holmes I want to set the title from outside of the dialog class (where I create the instance). I.e. MyDialogFragment myFragment = new MyDialogFragment(); myFragment.getDialog().setTitle("myTitle"); myFragment.show(getFragmentManager(), "myTitle"); However, it seems as though getDialog() returns null at this point. Is there another way to set the title from here (without making my own setTitle methdo)? Sep 17, 2013 at 12:00
  • 1
    I believe Jason's approach bellow is more correct in the general case.
    – Michel
    Jan 6, 2015 at 19:13
75

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;
}
3
15

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.

1
  • Setting android:windowNoTitle false wasn't necessary after switching to Android.Support.V7.App.AppCompatDialogFragment from Android.Support.V4.App.DialogFragment, nor sufficient as it resulted in duplicate titles (even though I ensured it was being set only once).
    – samus
    Mar 11, 2019 at 19:41
2

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);
    }
}
1

You can take a look at the official docs. The way i did is like this:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
            .setTitle("My Title");
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.my_layout, null);
    builder.setView(view);

    return builder.create();
}
1

Similar to Ban Geoengineering's answer, but with a few modifications, so instead of coding what specific theme to use in the DialogFragment, I override the default style used by DialogFragments in my styles.xml.

set the title in the androidx.fragment.app.DialogFragment.

class EditBatteryLevelFragment:DialogFragment(),SelfClosingFragment.Host
{
    override fun onCreateView(
        inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?
    ):View
    {
        // set dialog title
        requireDialog().setTitle(R.string.edit_battery_level__title)

        // .....
        return someView
    }
}

in your app theme in styles.xml, override android:dialogTheme, which is the default style used by DialogFragment instances.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.MaterialComponents">

        <!-- BONUS READING: override material colors here, too https://material.io/develop/android/theming/color -->

        <!-- override DialogFragment theme of those spawned by activities with this theme -->
        <item name="android:dialogTheme">@style/AppTheme.Dialog</item>

    </style>

    <!-- ... -->

also in styles.xml, declare the dialog theme that will be used by DialogFragment instances. it's important for this style to inherit from ThemeOverlay so that it will preserve your app's theme colors.

    <!-- ... -->

    <!-- define the style for your dialog -->
    <style name="AppTheme.Dialog" parent="ThemeOverlay.MaterialComponents.Dialog">

        <!-- add a minimun width to the dialog, so it's not too narrow -->
        <item name="android:windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>

        <!-- display the title for dialogs -->
        <item name="android:windowNoTitle">false</item>

    </style>

</resources>

make sure that the activity that is spawning the DialogFragment is using the defined AppTheme.

1
  • 1
    Perfect answer! I only needed <item name="android:windowNoTitle">false</item> to show the title and make the dialog full width.
    – nhcodes
    Jan 9, 2021 at 2:40
0

If you are using view binding:

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    binding = YourDialogXmlBinding.inflate(getLayoutInflater());
    AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
    builder.setTitle("Your title here")
            .setView(binding.getRoot());
    return builder.create();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.