Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong button style when creating AlertDialog with androidx DialogFragment

Here is my root style:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

And here is how I create dialog:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        return AlertDialog.Builder(requireActivity())
            .apply {

                setMessage(R.string.dialog_delete_service_message)
                setPositiveButton(
                    R.string.dialog_delete_service_positive_button
                ) { _, _ ->
                    listener?.onConfirmed()
                }
                setNegativeButton(R.string.dialog_delete_service_negative_button, null)

            }.create()
    }

Fragment is subclass of androidx.fragment.app.DialogFragment.

Here is how dialog is shown:

enter image description here

I want to show borderless buttons but dialog buttons are bordered. Is any style missing?

like image 785
Misagh Emamverdi Avatar asked Aug 26 '19 08:08

Misagh Emamverdi


People also ask

How many button options can you use in creating AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How do I change the color of my AlertDialog 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.


1 Answers

Use the MaterialAlertDialogBuilder

new MaterialAlertDialogBuilder(context)
            .setTitle("xxxx")
            .setMessage("...")
            .setPositiveButton("..", null)
            .show();

and use this style in your app theme:

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
 </style>

enter image description here

like image 95
Gabriele Mariotti Avatar answered Sep 29 '22 10:09

Gabriele Mariotti