Change button color in AlertDialog

后端 未结 16 2304

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");
    }
    
    0 讨论(0)
  • 2020-12-02 22:50

    we can change alert dialog button text colour using Style.

     AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.yourDialog);
        dialog.setTitle(R.string.title);
        dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //code here 
            }
        });
        dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //do here 
            }
        });
    
        dialog.show();
    

    Style.xml

    <style name="yourDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    
        <item name="android:colorAccent">@color/themeColor</item>
        <item name="android:colorPrimary">@color/themeColor</item>
    
    </style>
    
    0 讨论(0)
  • 2020-12-02 22:50

    no you cant change the color or images or background of the default buttons of alert boxes. For customization you will need to make you on custom dialog box like this.

    public class TryAgainAlert extends Dialog implements OnClickListener
    {
        @Override
     public boolean onKeyDown(int keyCode, KeyEvent event)
     {
      if (keyCode == KeyEvent.KEYCODE_BACK)
      {   
    
       Intent i = new Intent(getApplicationContext(), MainMenu.class);
       finish();
       startActivity(i);
    
       return true;
      }
      return super.onKeyDown(keyCode, event);
     }
    
    
        TextView scores;
        Button tryagain,mainmenu,submit;
    
    
         public TryAgainAlert(Context context) {
            super(context);
    
            setContentView(R.layout.tryagainalert);
    
            scores=(TextView)findViewById(R.id.text);
    
    
    
            tryagain= (Button) findViewById(R.id.trya);
            mainmenu= (Button) findViewById(R.id.submitscore);
            submit= (Button) findViewById(R.id.mainmenu);
    
        }
    
    
        @Override
        public void onClick(View v) {
            if(v == tryagain)
            {
    
            else if (v==mainmenu)
            {
    
    
            }
            else if (v == submit)
            {
    
            }
        }
    
    }
    

    you can do what ever you want with the XML file. I hope it will help. Thanks

    0 讨论(0)
  • 2020-12-02 22:52

    Here is some example :

    AlertDialog.Builder b = new AlertDialog.Builder(all.this);
    
    b.setMessage("r u wan't 2 exit");
    b.setCancelable(false);
    
    b.setNegativeButton("no", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();    
        }
    });
    
    b.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent i=new Intent(getBaseContext(), s.class);
            startActivity(i);
        }
    });
    
    AlertDialog a=b.create();
    
    a.show();
    
    Button bq = a.getButton(DialogInterface.BUTTON_NEGATIVE);  
    bq.setBackgroundColor(Color.BLUE);
    
    0 讨论(0)
  • 2020-12-02 22:52

    I think there are developer who wish to extend the AlertDialog class and define the buttons colors withing class definition. For such purpose you can use this code:

    class MyDialog extends AlertDialog {
        public MyDialog(final Context context) {
            super(context); 
            setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);  
                    Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);
    
                    negativeButton.setBackgroundColor(Color.GREEN);
                    positiveButton.setBackgroundColor(Color.RED);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-02 22:52

    if you are using DialogFragment ( android.app.DialogFragment ) then you can overwrite onStart method to get handle of all the buttons (Positive, Negative and Neutral).

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
                                                null);
    
        builder.setTitle(getLocalizedString("edit_event"))
                .setView(eventEditDialogView)
                .setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
                .setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
        return builder.create();
    }
    
     @Override
        public void onStart() {
            super.onStart();
        Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextColor(Color.BLACK);
        positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
    }
    

    All the above solutions will work with AlertDialog or Dialog created on same activity or Fragment but not on DialogFragment created separately.

    0 讨论(0)
提交回复
热议问题