How to change the color of the buttons in AlertDialog?

At the moment, the dialogue looks like this

In the markup dialogue ask

android:background="@color/background_window" 

everything except the buttons is highlighted in gray

I try this:

 dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(ContextCompat.getColor(MyApplication.getAppContext(), R.color.background_window)); 

the application crashes when you open a dialog

Thank you in advance

    2 answers 2

    The solution above repaints only the background of the buttons.

    enter image description here

    In essence, the answer to the question was commented in the answer above.

    Before opening the dialog, insert the line:

     dialog.getWindow().setBackgroundDrawableResource(R.color.background_window); 

    and it all worked:

    enter image description here

    It is strange that the line in the markup does not set the background for the button bar

      On en-SO they write that you can hang up a listener showing the dialogue and already in it change the background of the buttons, since At this point, the dialogue has already been created.

       AlertDialog.Builder adb = new AlertDialog.Builder(context1); adb.setTitle(context1.getString(R.string.app_name)) .setMessage(message) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = adb.create(); // Make some UI changes for AlertDialog dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(final DialogInterface dialog) { // Add or create your own background drawable for AlertDialog window //Window view = ((AlertDialog)dialog).getWindow(); //view.setBackgroundDrawableResource(R.drawable.your_drawable); // Customize POSITIVE, NEGATIVE and NEUTRAL buttons. Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE); positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); positiveButton.setTypeface(Typeface.DEFAULT_BOLD); positiveButton.invalidate(); Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE); negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); negativeButton.setTypeface(Typeface.DEFAULT_BOLD); negativeButton.invalidate(); Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL); neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); neutralButton.setTypeface(Typeface.DEFAULT_BOLD); neutralButton.invalidate(); } });