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
First, it's better to use use MaterialAlertDialog if you are using Material Theme.
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.
Step 2: Tell Android that you want to alter a specific button style.
buttonBarNeutralButtonStyle
,buttonBarNegativeButtonStyle
orbuttonBarPositiveButtonStyle
Step 3: Define your custom style
Found another solution for this with using MaterialComponents here: https://issuetracker.google.com/issues/116861837#comment9
Though it is still not "intended behavior" to me.
I tested the above answers. Although I got a good idea, none worked for my case. So, this is my answer.
Make sure to have
android:theme="@style/AppMaterialTheme"
in your manifest file under Application or Activity.Open your Styles.xml file and change it based on the following.
You won't need to apply the theme to your AlertDialog as your Activity applies the theme to it. So, create the dialog normally.
The result will be.
If you don't want to use
androidx.appcompat.app.AlertDialog
, you can just redefine the style of the dialog buttons:In your style.xml :
I figured out what was causing this problem. I need to use different AlertDialog class:
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
If you are using the Material Components library the best way to have an
AlertDialog
is to use theMaterialAlertDialogBuilder
.It is the default result: