Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialComponents theme alert dialog buttons

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: enter image description here

How can I make them borderless and backgroundless again?

PS I am using alert builder to create alert dialogs:

android.app.AlertDialog.Builder 
like image 347
antanas_sepikas Avatar asked Oct 16 '18 07:10

antanas_sepikas


People also ask

How do I change the color of my dialog alert positive button?

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.

How do I show alerts in dialog?

setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.

How many action button can a dialog have?

There should be no more than three action buttons in a dialog.

How do I turn off alerts in 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.


2 Answers

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

like image 148
antanas_sepikas Avatar answered Sep 28 '22 10:09

antanas_sepikas


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> 

Screenshot

like image 31
Rob Avatar answered Sep 28 '22 10:09

Rob