'How to set button and text color in Android Alert Dialog
I am modifying code for an Android app. The code generates an Alert Dialog, and should present the user with two buttons. However, when the Alert Dialog is shown, the two buttons / options are not visible.
I read other posts that suggest that the text and background colours are both white. So the suggestion is to modify the text colour etc. All the examples I can find suggest something like the following:
val builder: AlertDialog.Builder = Builder(this, .style.DialogTheme)
where the style is defined like this:
<style name="MyTheme">
<item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>
However, the code I am trying to modify is formatted differently, and I don’t know how to modify it to change the text colour or button background:
runOnUiThread {
alert {
title = "My title"
message = "My message"
isCancelable = false
positiveButton(android.R.string.ok) {
// do something
}
negativeButton(android.R.string.cancel) {
// do something else
)
}
}.show()
}
I have spent all morning searching and trying out solutions (including the ones the SO suggest, as I type in my question) but I am just can’t get anything to work. Any suggestions?
Thanks in advance
Garrett
Solution 1:[1]
In the end, I just restructured the code similar to so many of the examples online.
For the alert:
runOnUiThread {
val builder = AlertDialog.Builder(this, R.style.AlertDialogCustom)
builder.setMessage("My message")
.setPositiveButton(android.R.string.ok,
DialogInterface.OnClickListener { dialog, id ->
// do something
})
.setNegativeButton(android.R.string.cancel,
DialogInterface.OnClickListener { dialog, id ->
// do something else
})
.setTitle("My title")
val alertDialog: AlertDialog = builder.create()
// Set other dialog properties
alertDialog.setCancelable(false)
alertDialog.show()
}
And the styling to look after colr etc.:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingRight">2dp</item>
<item name="android:paddingLeft">2dp</item>
<item name="android:textSize">18dp</item>
<item name="android:background">@color/colorWhite</item>
<item name="android:colorAccent">@color/colorYellow</item>
</style>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | garrettb |
