Search code examples
androidandroid-alertdialogmaterial-components-android

Full width alert dialog buttons


I'm trying to get my AlertDialogs to look like this, with the full width button:

enter image description here

I'm following the Google's documentation and it says to use this style:

 R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_FullWidthButtons

but I don't have access to that theme:

enter image description here

I have the appropriate dependency added in the project's Gradle file:

implementation 'com.google.android.material:material:1.5.0'

I feel like I'm just missing something obvious.


Solution

  • The implementation of the theme overlays is missing in the library.
    You can define it on your own, by:

    Declare a full width button style, like:

      <style name="Button.FullWidth" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:maxWidth">@null</item>
      </style>
    

    Declare the theme overlay with full-width button styles:

      <style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.FullWidthButtons">
        <item name="materialAlertDialogButtonSpacerVisibility">2</item>
        <item name="buttonBarPositiveButtonStyle">@style/Button.FullWidth</item>
        <item name="buttonBarNegativeButtonStyle">@style/Button.FullWidth</item>
        <item name="buttonBarNeutralButtonStyle">@style/Button.FullWidth</item>
      </style>
    

    enter image description here