Search code examples
androidandroid-alertdialogtextcoloralertdialogpro

AlertDialog looks differently on Nexus devices


I'm creating different AlertDialogs in different scenarios in my application using the following code:

public static void showAlertDialog(Context activityContext, DialogType type, CharSequence title, CharSequence msg, CharSequence posText,
        DialogInterface.OnClickListener posOnClickListener, CharSequence negText, DialogInterface.OnClickListener negOnClickListener, boolean isCancelable, int iconResId) {
    try {
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activityContext);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) title))
            alertDialogBuilder.setTitle(title);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) msg))
            alertDialogBuilder.setMessage(msg);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) posText))
            alertDialogBuilder.setPositiveButton(posText, posOnClickListener);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) negText))
            alertDialogBuilder.setNegativeButton(negText, negOnClickListener);

        alertDialogBuilder.setCancelable(isCancelable);

        // set alert icon
        if (iconResId == 0) {
            alertDialogBuilder.setIcon(type == DialogType.ERROR ? android.R.drawable.ic_dialog_alert : android.R.drawable.ic_dialog_info);
        } else {
            alertDialogBuilder.setIcon(iconResId);
        }

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();
    } catch (Exception e) {
        InfiLogger.w("showAlertDialog", e.toString(), e);
        Crashlytics.logException(e);
    }
}

While on most devices that run Lollipop or higher the AlertDialog looks like this:

enter image description here

On Nexus devices the same running code looks like this:

enter image description here

Does someone knows why this happens? And what is the proper way to fix this?


Solution

  • The solution for this problem is using the AlertDialog from the support library instead of using the original one.

    So all I had to do to solve this issue was to replace this import:

    import android.app.AlertDialog;
    

    with this import:

    import android.support.v7.app.AlertDialog;