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
Theme.MaterialComponents.DayNight.DarkActionBar - Dialog's button text uses colorPrimary
Theme.Material3.DayNight.NoActionBar - Dialog's button text uses colorPrimary
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()
}
}