8

This is a picture of an AlertDialog that is shown within my app. It should have a deny and an accept button.

As you can see it has not:

enter image description here

I cannot reproduce this error as I dont have a phone with Android 7.1. The picture was taken on a Google Pixel and send to me.

All other Android versions this App was tested upon did not encounter this bug. (Versions 4.1, 6.0.1)

Here is code of the method creating the dialog:

  /**
 * Creates a 2 options dialog.
 * @param context
 * @param title headline of the dialog
 * @param message main text of the dialog
 * @param accept listener for the accept button
 * @param deny listener for deny button
 * @param acceptText text of the positive answer button
 * @param denyText text of the negative answer button
 * @param cancelable weather a click to anywhere but the presented buttons dismisses the dialog
 * @return a created dialog instance. To display it call show()
 */
public static AlertDialog createAcceptDenyDialog(Context context,
                                                 String title, String message, String acceptText,
                                                 String denyText, boolean cancelable,
                                                 DialogInterface.OnClickListener accept,
                                                 DialogInterface.OnClickListener deny,
                                                 DialogInterface.OnDismissListener dismiss){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(acceptText, accept)
            .setNegativeButton(denyText, deny)
            .setCancelable(cancelable)
            .setOnDismissListener(dismiss);
    return alertDialog.create();
}

This is the code causing the dialog to be displayed:

public void showRequestErrorRetryDialog(String title, String message) {
    Dialog dialog  = DialogFactory.createAcceptDenyDialog(this
            , title
            , message
            , getString(R.string.retry_button)
            , getString(R.string.abort_button)
            , true
            , (dialogInterface, i) -> {
                onStartServerCommunication();
                showProgressOverlay();
            }
            , null
            , null);
    dialog.show();
}

As you can see, I use retrolambda.

Does anyone have an idea what happens?

zetain
  • 361
  • 4
  • 15
  • Maybe you are passing the wrong context? What is `this` in your method? What works fine for me in Android 7 is `new AlertDialog.Builder(context).attributes.show()`.. (Attributes are all methods like `.setTitle()` `.setPositiveButton()` etc.. – creativecreatorormaybenot Jan 14 '17 at 21:39
  • The Method showRequestErrorRetryDialog is called from within the activity in which the dialog should be shown. "this" is therefore the context of the activity – zetain Jan 14 '17 at 22:31
  • this is the activity then? Why do you not use getApplicationContext() ? – creativecreatorormaybenot Jan 14 '17 at 23:26
  • Is this prefered to using the activity context when creating dialogs? Because their is no specific reason. Could you elaborate? – zetain Jan 15 '17 at 11:49
  • I was just wondering why you are using it over getApplicationContext . As I said I don't really see a reason for your problem so you might need to be creative. My version works finde for me on Nougat so yeah – creativecreatorormaybenot Jan 15 '17 at 15:46
  • 1
    You need to explicitly set the theme, see here: http://stackoverflow.com/questions/39621606/missing-buttons-on-alertdialog-android-7-0-nexus-5x – netcyrax Jan 16 '17 at 14:17
  • 1
    Possible duplicate of [Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)](https://stackoverflow.com/questions/39621606/missing-buttons-on-alertdialog-android-7-0-nexus-5x) – Shirish Herwade Nov 21 '17 at 11:27

1 Answers1

17

The solution which is working for me was to add the following lines on my style.xml :

// your main style
<style name="YourStyleName" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

// dialog style
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
</style>

// button's dialog style
<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/colorPrimary</item>
</style>

It's working perfectly, I hope it will help you guys.

tryp
  • 1,120
  • 20
  • 26
  • It works if you use `Android.Support.V7.App.AlertDialog`. For me defining the following style was enough`` – AZ_ Feb 18 '20 at 11:02