Android DialogFragment title not showing
Asked Answered
U

3

42

When creating a custom DialogFragment, i set the title using the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_dialog_add, container, false);      

    // Setting title here
    getDialog().setTitle("Add New");

    return v;
}

The above code works fine for me on API level older than 23. For API 23 the title is not showing at all.

Any idea why? and how to make the title show on API 23?

Unorthodox answered 4/9, 2015 at 19:10 Comment(0)
U
58

Solved by adding the following to the styles.xml:

<item name="android:dialogTheme">@style/CustomDialog</item>

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>
Unorthodox answered 22/9, 2015 at 8:20 Comment(4)
Thank you! I was pulling my hair out. I'm not sure why windowNoTitle appears to default to true after API 23 but thanks for the fix!Galliett
Really don't understand why they changed this in API23Operculum
Use parent="@style/Theme.AppCompat.Dialog" for dark dialog.Chancellorship
My base theme extends Theme.AppCompat.DayNight.NoActionBar. If I override this, then background, control color all changes. Can you suggest any other solution?Pokorny
L
47

In your styles:

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

If your dialog is a fragment:

MyFragment myFragment = new MyFragment();
myFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
Leighannleighland answered 20/10, 2015 at 23:35 Comment(0)
B
-1

I solved using .setMessage instead of .setTitle. I know it's not the same appearance but in my case, I just needed a hint for the user.

Brendonbrenk answered 11/8, 2020 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.