0

I expected the dialog's button text to use colorAccent.

However, in some app themes, the dialog's button text uses colorPrimary.

Here are my findings:


Theme.AppCompat.Light.DarkActionBar - Dialog's button text uses colorAccent

enter image description here


Theme.MaterialComponents.DayNight.DarkActionBar - Dialog's button text uses colorPrimary

enter image description here


Theme.Material3.DayNight.NoActionBar - Dialog's button text uses colorPrimary

enter image description here


I was wondering how I can enforce the use of colorAccent for the dialog's button text, regardless of the app theme.

Here's the testing code I am using:

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->

    <!-- Theme.Material3.DayNight.NoActionBar = Dialog button use colorPrimary -->
    <!-- Theme.MaterialComponents.DayNight.DarkActionBar = Dialog button use colorPrimary -->
    <!-- Theme.AppCompat.Light.DarkActionBar = Dialog button use colorAccent -->

    <style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="colorPrimary">#ff0000</item>    <!-- red -->
        <item name="colorAccent">#ff1778f2</item>   <!-- blue -->
    </style>

    <style name="Theme.MyApplication" parent="Base.Theme.MyApplication" />
</resources>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        showDialog()
    }

    private fun showDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Confirmation")
        builder.setMessage("Do you want to proceed?")

        // Add OK button
        builder.setPositiveButton("OK") { dialog, which ->
            // Handle OK button click here
        }

        // Add Cancel button
        builder.setNegativeButton("Cancel") { dialog, which ->
            // Handle Cancel button click here
            dialog.dismiss()
        }

        // Create and show the AlertDialog
        val alertDialog = builder.create()
        alertDialog.show()
    }
}
1
  • have you tried using compose theme? if you can create separate screen which uses compose and theme it. or you are restricted to use xml only?
    – Aks4125
    Commented May 1 at 6:10

3 Answers 3

2
+500

The best approach is to change this dynamically. Check below code which uses colorAccent for dialog button text color.

You can create extension function of your AlertDialog to be use anywhere in app.

AlertDialog : import androidx.appcompat.app.AlertDialog

private fun AlertDialog.showDialog() {
    this.show()
    this.getButton(AlertDialog.BUTTON_NEGATIVE)
        .setTextColor(getColor(R.color.colorAccent))
    this.getButton(AlertDialog.BUTTON_POSITIVE)
        .setTextColor(getColor(R.color.colorAccent))
}

Code Reference:

enter image description here

0

Don't specify the whole app's primaryColor just to style your dialog. Use the theme attribute that is for Alert Dialog intead. Refer to below code to see to change the button color of you Dialog:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="alertDialogTheme">@style/MyDialog</item>
    </style>

    <style name="MyDialog" parent="ThemeOverlay.Material3.Dialog.Alert">
        <item name="buttonBarButtonStyle">@style/ButtonBarStyle</item>
    </style>

    <style name="ButtonBarStyle" parent="Widget.Material3.Button.TextButton.Dialog.Flush">
<!--        Specify your color accent instead-->
        <item name="android:textColor">#1C0808</item>
    </style>
</resources>
0
 private fun showMessageOKCancel(message: String, okListener: DialogInterface.OnClickListener) {
       val alert =  AlertDialog.Builder(requireContext())
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", null)
            .create()

        alert.show()
        alert.makeButtonTextBlue()


    }
       
    fun AlertDialog.makeButtonTextBlue() {
            this.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.joker_green_400))
            this.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.joker_green_400))
        }

These functions will assist in changing the text color.

Not the answer you're looking for? Browse other questions tagged or ask your own question.