How to set the title of DialogFragment?
Asked Answered
U

7

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.

Undersigned answered 4/3, 2011 at 12:31 Comment(0)
H
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;
    }
    // ...
}
Halhalafian answered 7/4, 2011 at 22:25 Comment(7)
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.Undersigned
What if I wanted to change the background of the title. Is that possible?Haemostatic
Also note that you can't set the dialog title in the fragment's onCreate() method since the dialog hasn't been created yet :)Dereliction
@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)?Agony
Also comment out line in onCreateDialog method: //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);Extine
I believe Jason's approach bellow is more correct in the general case.Eidson
If you has spent several hours to figure out why this accepted answer doesn't work, you need to combine it together with #32405542Solid
S
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;
}
Subtract answered 4/3, 2011 at 13:22 Comment(3)
Your solution is better because fragment can be used not only in dialogsChery
This solutions is also useful for xamarin.androidCalefaction
Also see my more recent answer which is based upon the above code: https://mcmap.net/q/143949/-how-to-set-the-title-of-dialogfragmentOverstretch
O
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.

Overstretch answered 23/1, 2017 at 1:26 Comment(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).Wojak
R
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);
    }
}
Ravenous answered 17/2, 2018 at 2:35 Comment(0)
I
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();
}
Incompliant answered 8/11, 2017 at 12:39 Comment(0)
T
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.

Theaterintheround answered 17/12, 2020 at 0:22 Comment(1)
Perfect answer! I only needed <item name="android:windowNoTitle">false</item> to show the title and make the dialog full width.Kilmer
G
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();
}
Garrotte answered 13/4, 2022 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.