5

I want to use new material outlined buttons with default alert dialog.

I have created style in style.xml as below

<style name="OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="strokeColor">@color/colorAccent</item>
    <item name="strokeWidth">2dp</item>
</style>

<style name="MaterialDialogStyle" parent="Theme.MaterialComponents.Dialog.Alert">
    <item name="android:textColorPrimary">@color/colorAccent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorPrimary">@color/colorAccent</item>
    <item name="buttonStyle">@style/OutlinedButton</item>
</style>

I am using new Material Components theme to style Yes and No Buttons.

Now I use above style in my code by setting it to AlertDialog builder.

AlertDialog.Builder builder = new AlertDialog.Builder(ProductListActivity.this, R.style.MaterialDialogStyle);

But the output is as below enter image description here

Is there a way to use latest material outlined buttons with Default Alert Dialog? I am using Material components from design support library if it makes any difference.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
karan
  • 8,637
  • 3
  • 41
  • 78
  • 2
    Duplicate? : https://stackoverflow.com/questions/52829954/materialcomponents-theme-alert-dialog-buttons – ʍѳђઽ૯ท Nov 22 '18 at 10:40
  • @Mohsen: in that question user is using androidx, I am using material components from design support library, will it be still similar? – karan Nov 22 '18 at 10:45
  • Switch to `AndroidX` then import the androidX import. After, add [material design dependency](https://material.io/develop/android/docs/getting-started/) to see the result. I guess this will work better with the new AndroidX and the Material Design dependency added. – ʍѳђઽ૯ท Nov 22 '18 at 10:49
  • Is there any specific requirement to switch, because material components are available with support library also. – karan Nov 22 '18 at 10:50
  • I guess the current import causes the issue since both material components and support library are different. Anyways, you're right about the second part, (`are available with support library`) Why not checking with `v7 AlertDialog` import too? or simply, using AndroidX dependency-import? – ʍѳђઽ૯ท Nov 22 '18 at 10:58

1 Answers1

3

Since you are using a Material Theme you can use:

      new MaterialAlertDialogBuilder(MainActivity.this, R.style.MyMaterialAlertDialog)
          .setTitle("Clear cart")
          .setMessage("Pressing back...")
          .setPositiveButton("YES", null)
          .setNegativeButton("NO", /* listener = */ null)
          .show();

And then define the style:

  <style name="MyMaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarPositiveButtonStyle">@style/OutlinedButton</item>
    <item name="buttonBarNegativeButtonStyle">@style/OutlinedButton</item>
  </style>

  <style name="OutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="strokeColor">@color/colorAccent</item>
    <item name="strokeWidth">2dp</item>
  </style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841