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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
StefanK
  • 2,020
  • 2
  • 14
  • 16

7 Answers7

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;
    }
    // ...
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Rob Holmes
  • 3,768
  • 1
  • 16
  • 19
  • 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 08 '11 at 11:53
  • 1
    What if I wanted to change the background of the title. Is that possible? – MinceMan Jan 22 '12 at 02: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 '12 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)? – David Doria Sep 17 '13 at 12:00
  • Also comment out line in onCreateDialog method: //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); – David Douglas Mar 19 '14 at 14:12
  • 1
    I believe Jason's approach bellow is more correct in the general case. – Michel Jan 06 '15 at 19:13
  • If you has spent several hours to figure out why this accepted answer doesn't work, you need to combine it together with https://stackoverflow.com/questions/32405042/android-dialogfragment-title-not-showing – Cheok Yan Cheng Jun 01 '18 at 20:12
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;
}
Jason Hanley
  • 3,225
  • 2
  • 24
  • 21
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.

Community
  • 1
  • 1
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • 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 '19 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);
    }
}
Anatolii Shuba
  • 4,614
  • 1
  • 16
  • 17
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();
}
Tahir Ferli
  • 636
  • 4
  • 16
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.

Eric
  • 16,397
  • 8
  • 68
  • 76
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();
}
Taha
  • 117
  • 1
  • 11