How to change the colour of Positive and negative button in Custom Alert dialog in android

前端 未结 5 1693
既然无缘
既然无缘 2020-12-29 07:12

What i am doing: I am creating a custom alert dialog

What i am trying to do: along with below code, How to change the color of act

5条回答
  •  醉酒成梦
    2020-12-29 07:15

    you can do it like this-

    public void createDialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Do you want to exit from app");
        builder.setCancelable(false);
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "You exit from app",
                        Toast.LENGTH_LONG).show();
    
            }
        });
    
        AlertDialog alert = builder.create();
        alert.show();
        Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        nbutton.setBackgroundColor(Color.MAGENTA);
        Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        pbutton.setBackgroundColor(Color.YELLOW);
    }
    

提交回复
热议问题