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

假装没事ソ 提交于 2019-12-18 11:49:48

问题


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 action buttons in dialog(positive and negative)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();

回答1:


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);
}



回答2:


AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogeTheme);

<style name="DialogeTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/all_item_bg</item>
    <item name="colorAccent">@android:color/white</item>
</style>



回答3:


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));
}



回答4:


//***The simpliest solution is:***

dialog.show(); //Only after .show() was called
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor);



回答5:


You can use the setOnShowListener method of the AlertDialog to get the buttons and apply a different color and their own listeners, here you have an example in Kotlin:

val alert : AlertDialog?
val alertView = LayoutInflater.from(context).inflate(R.layout.alert_add_product, null, false)

val builder = AlertDialog.Builder(context)
    builder.setCancelable(false)
    builder.setView(alertView)

    /**Create positive and negative buttons**/
    builder.setPositiveButton(android.R.string.ok, null)
    builder.setNegativeButton(android.R.string.cancel, null)

    alert = builder.create()

    /**Listener called when the AlertDialog is shown**/
    alert.setOnShowListener {

         /**Get the positive button from the AlertDialog**/
         val positiveButton = alert.getButton(DialogInterface.BUTTON_POSITIVE)

         /**Set your color to the positive button**/
         positiveButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))

         /**Set listener to the positive button**/
         positiveButton.setOnClickListener({
              alert.dismiss()
         })

         /**Get the negative button from the AlertDialog**/
         val negativeButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE)

         /**Set your color to the negative button**/
         negativeButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))

         /**Set listener to the negative button**/
         negativeButton.setOnClickListener {
              alert?.dismiss()
         }
    }

    alert.show()


来源:https://stackoverflow.com/questions/27520967/how-to-change-the-colour-of-positive-and-negative-button-in-custom-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!