How to set the title of DialogFragment?

AndroidAndroid FragmentsAndroid Dialogfragment

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefanKView Question on Stackoverflow
Solution 1 - AndroidRob HolmesView Answer on Stackoverflow
Solution 2 - AndroidJason HanleyView Answer on Stackoverflow
Solution 3 - Androidban-geoengineeringView Answer on Stackoverflow
Solution 4 - AndroidAnatolii ShubaView Answer on Stackoverflow
Solution 5 - AndroidTahir FerliView Answer on Stackoverflow
Solution 6 - AndroidEricView Answer on Stackoverflow
Solution 7 - AndroidTahaView Answer on Stackoverflow