Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

android - Change dialog button color

Is there any way how to change default dialog buttons colour, or do I need to do custom dialog for that?

This is my dialog:

private void removeItem(final int position) {
    /** Create dialog which ask if user is sure about delete name from list **/
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Delete player");
    builder.setIcon(R.mipmap.ic_launcher);
    builder.setMessage("Do you want to delete player: "" + mNameList.get(position).getText1() + ""?")
            .setCancelable(false)

            /** If user click "ok" button, delete player from list and save changes **/
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mNameList.remove(position);
                    mAdapter.notifyItemRemoved(position);
                    saveData();
                }
            })

            /** If user click "cancel" button, name won't delete from list **/
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

    /** Create Dialog **/
    AlertDialog alert = builder.create();
    alert.show();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the MaterialAlertDialogBuilder provided by the Material Components library which allows the creation of a Material AlertDialog.

Just use:

   new MaterialAlertDialogBuilder(MainActivity.this,
       R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setMessage("Lorem ipsum dolor ....")
              .setPositiveButton("Ok", /* listener = */ null)
              .setNegativeButton("Cancel", /* listener = */ null)
              .show();

Then define your custom style, using the buttonBarPositiveButtonStyle and buttonBarNegativeButtonStyle attributes:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
     <item name="buttonBarNeutralButtonStyle">....</item>
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="backgroundTint">@color/secondaryColor</item>
  </style>

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...