Change button color in AlertDialog

后端 未结 16 2312

How can I change the color of the button(s) in an AlertDialog in Android?

16条回答
  •  伪装坚强ぢ
    2020-12-02 22:49

    Here is the perfect solution that worked for me:

    AlertDialog.Builder alertDialogBuilder
                            = new AlertDialog.Builder(DashboardActivity.this);
    alertDialogBuilder.setTitle("");
    alertDialogBuilder.setMessage("Are you sure you want to Logout?");
    
    alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            preferenceManager.logout();
            Intent intent = new Intent(DashboardActivity.this,
                                    LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    });
    
    alertDialogBuilder.setNegativeButton("Cancel", null);
    
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    
    Button btnOk = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    Button btnCancel = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    
    if (btnOk != null && btnCancel != null) {       
        btnOk.setTextColor(getResources().getColor(R.color.colorGreenButton));
        btnCancel.setTextColor(getResources().getColor(R.color.colorGreenButton));
    } else {
        Log.i(TAG, "LogOut: Buttons of Dialog are null");
    }
    

提交回复
热议问题