Android 6.0 Dialog text doesn't appear

23,337

Solution 1

Use constructor with theme for Lollipop and newer android versions:

Dark theme

    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }

And for Light theme

    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }

Solution 2

I've come across a few answers that say that you should use the following in your app theme:

<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>

While that is correct, it wasn't working in all places for me. Eventually, I realized that in some places I was using the regular AlertDialog builder and in other places I was using the support builder. If you are using the support AlertDialog, make sure to also include the following in your theme:

<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>

Solution 3

Just put this in your styles file inside your Base Theme style tag and you are good to go

<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>

Solution 4

Answer for 2), we need to call setStyle() before onCreateDialog() because theme is used in onCreateDialog()

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog_Alert);
    }
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setTitle(R.string.dialog_title);
    return dialog;
}

Solution 5

i tried to fix this problem by making a style of my own.

your dialog object should be like this and your style pointed to this Dialog will be written below

new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))
            .setTitle("Title")
            .setMessage("Sample Message ...").show();

R.style.Dialog ::

<style name="Dialog">
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColor">@color/primary_color</item>
</style>

Colors ::

<color name="primary_text">#212121</color>
<color name="primary_color">#2196f3</color>

finally the output should be something like this

Note : "android:textColorPrimary" goes for dialog setMessage and "android:textColor" goes for dialog setTitle ; i do not use setPositive and setNegative button and listener in my dialog object but you can see them in the picture if you want you can make your own dialog object.

Share:
23,337
Veka
Author by

Veka

Updated on July 11, 2022

Comments

  • Veka
    Veka almost 2 years

    I updated my phone to Android 6.0 and I have these 2 problems with dialogs:

    1)The title is shown but the messages isn't for alert dialog(SOLVED):

            new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");
    

    2)Also custom dialog fragment's title is not shown(NOT SOLVED):

            getDialog().setTitle("Title");
    

    There was not such a problem in lollipop or in older versions, the problem appeared only after updating my phone to marshmallow.

    How to solve the problem?