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
You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles. xml and all the dialog boxes will be updated in the whole application.
setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.
There should be no more than three action buttons in a dialog.
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With