4
votes

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.

1
@Mohsen: in that question user is using androidx, I am using material components from design support library, will it be still similar? - karan
Switch to AndroidX then import the androidX import. After, add material design dependency to see the result. I guess this will work better with the new AndroidX and the Material Design dependency added. - ʍѳђઽ૯ท
Is there any specific requirement to switch, because material components are available with support library also. - karan
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? - ʍѳђઽ૯ท

1 Answers

2
votes

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