MaterialComponents theme alert dialog buttons

material design alert dialog android github
android-alertdialog button style
android dialog theme
material components-android
theme appcompat light dialog alert
theme materialcomponents button background
confirmation dialog android
materialalertdialogbuilder gradle

Recently I switched from support library to com.google.android.material:material:1.0.0

But now I have a problem, in this pages there's a note https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

Note: Using a Material Components theme enables a custom view inflater which replaces default components with their Material counterparts. Currently, this only replaces Button XML components with MaterialButton.

And the theme I am using

Theme.MaterialComponents.Light.NoActionBar

does exactly what it says in that note, it replaces AlertDialog Buttons to MaterialButtons but the problem is that by default MaterialButtons are colored background and now the buttons looks like this:

How can I make them borderless and backgroundless again?

PS I am using alert builder to create alert dialogs:

android.app.AlertDialog.Builder

I figured out what was causing this problem. I need to use different AlertDialog class:

androidx.appcompat.app.AlertDialog

When I switched to this everything started working as expected. Here's where I found the solution:

https://github.com/material-components/material-components-android/issues/162

AlertDialog has filled buttons when using "Theme.MaterialComponents.Light.​NoActionBar" #199. Closed. bjarn opened this issue on Sep 19,  PS I am using alert builder to create alert dialogs: android.app.AlertDialog.Builder android material-design android-alertdialog android-styles material-components-android

When using com.google.android.material:material:1.0.0 and androidx.appcompat.app.AlertDialog you can customize each button in the buttonBar by using Widget.MaterialComponents.Button.TextButton as parent.

val builder: AlertDialog.Builder = AlertDialog.Builder(ContextThemeWrapper(context, R.style.AlertDialogTheme))

Use the default layout or add a custom by builder.setView(R.layout.my_dialog)

In your styles:

<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="buttonBarPositiveButtonStyle">@style/Alert.Button.Positive</item>
    <item name="buttonBarNegativeButtonStyle">@style/Alert.Button.Neutral</item>
    <item name="buttonBarNeutralButtonStyle">@style/Alert.Button.Neutral</item>
</style>

<style name="Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="backgroundTint">@color/transparent</item>
    <item name="rippleColor">@color/colorAccent</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textAllCaps">false</item>
</style>

<style name="Alert.Button.Neutral" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="backgroundTint">@color/transparent</item>
    <item name="rippleColor">@color/colorAccent</item>
    <item name="android:textColor">@color/gray_dark</item>
    <item name="android:textSize">14sp</item>
</style>

Setting up a Material Components theme for Android Alert dialog: The simplest of dialogs, despite the name. Used to interrupt a user with urgent Characterized by a title, optional supporting text and action buttons. Currently, this only replaces Button XML components with MaterialButton. And the theme I am using Theme.MaterialComponents.Light.NoActionBar [&hellip;] Sign Up

If you are using the Material Components library the best way to have an AlertDialog is to use the MaterialAlertDialogBuilder.

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

It is the default result:

Create a custom alert dialog theme that inherited from ThemeOverlay. parent="​Widget.MaterialComponents.Button.TextButton.Dialog"> Hi, I just noticed that AlertDialog has filled buttons with the "Theme.MaterialComponents.Light.NoActionBar" theme. When I use the default "Theme.AppCompat.Light.NoActionBar" theme, AlertDialog has simple text buttons as expected.

Found another solution for this with using MaterialComponents here: https://issuetracker.google.com/issues/116861837#comment9

<style name="Theme.Custom.Material.Alert.Dialog.Light" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="materialButtonStyle">@style/Widget.AppCompat.Button.Borderless</item>
</style>

<style name="Theme.Custom.Material.Base.Light" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:dialogTheme">@style/Theme.Custom.Material.Alert.Dialog.Light</item>
    <item name="android:alertDialogTheme">@style/Theme.Custom.Material.Alert.Dialog.Light</item>
  ....
</style>

Though it is still not "intended behavior" to me.

In this tutorial, we'll be customizing Dialogs using Material Theme in our. Button​.Neutral</item> </style> <style name="Alert.Button.Positive" parent="Widget. Styled Buttons. We can style the buttons of MaterialAlertDialog since they are just MaterialButtons. Setting outline button/borderless button, ripple effects etc. Let’s see an example: <style name="AlertDialogTheme"> <item name="buttonBarPositiveButtonStyle">@style/Alert.Button.Positive</item> <item name="buttonBarNeutralButtonStyle">@style/Alert.Button.Neutral</item> </style> <style name="Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton"> <item

First, it's better to use use MaterialAlertDialog if you are using Material Theme.

MaterialAlertDialogBuilder(context)
            .setTitle(R.string.confirm)
            .setMessage(R.string.logout)
            .setPositiveButton(R.string.logout_alert_positive) { _, _ -> activity?.logout() }
            .setNegativeButton(R.string.never_mind, null)
            .show()

 

This is the layout.xml of the MaterialAlertDialog actions. As you can see there are 3 buttons and each has their own styles. So, here is how you can change them.

Step 1: Tell Android that you want to alter the default MaterialAlertDialog theme.

<style name="Base.AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="materialAlertDialogTheme">@style/AlertDialog</item>
    ...
</style>

Step 2: Tell Android that you want to alter a specific button style. buttonBarNeutralButtonStyle, buttonBarNegativeButtonStyle or buttonBarPositiveButtonStyle

<style name="AlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
</style>

Step 3: Define your custom style

<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:textColor">#FF0000</item>
</style>

And the theme I am using. Theme.MaterialComponents.Light.NoActionBar. does exactly what it says in that note, it replaces AlertDialog Buttons  There are four types of dialogs: 1. Alert dialog, 2. Simple dialog, 3. Confirmation dialog, 4. Full-screen dialog. Dialogs. A dialog is a type of modal window that appears in front of app content to provide critical information or ask for a decision.

I figured out what was causing this problem. I need to use different AlertDialog class: androidx.appcompat.app.AlertDialog. When I switched to  When scrolling is required, the dialog title is pinned at the top, with buttons pinned at the bottom. This ensures selected content remains visible alongside the title and buttons, even upon scroll. Dialogs don’t scroll with elements outside of the dialog, such as the background.

IDEA says that API 21+ is required to use colorAccent on Base.Theme.AppCompat.Light.Dialog.Alert using 'com.android.support:design:22.2.1' – Felipe Andrade Nov 24 '16 at 12:53

I have many alert dialogs in my app. It is a default layout but I am adding positive and negative buttons to the dialog. So the buttons get the default text color of Android 5 (green). I tried to c

Comments
  • You can use Bridge theme if you want AppCompat theme contents with some new Material UI styles, it can solve your problem, refer my this answer : stackoverflow.com/a/52401497/10271334
  • I migrated my code to androidx using the Android Studio's 'Migrate to AndroidX' option/feature but this library was not updated to AndroidX automatically. This issue for me was resolved when I updated the import manually as the answer suggests
  • You save me! Thanks!
  • This is what I was looking for when trying to style my dialogs. All answers I found were not using AndroidX.
  • I'm just not sure why you're setting up textAllCaps to false. I usually see it set to true on dialogs (the only exception was a Material Design example from google called Crane)
  • @LeonardoSibela Yea, default behaviour sets all buttons to textAllCaps=true, updated
  • I wish I could upvote this 50 times. I spent like two hours trying to just color stupid dialogs. Note that you should also use MaterialAlertDialogBuilder instead.
  • @gMale 's comment saved time for me.
  • yes but it's still in alpha no ?