Search code examples
androidkotlinandroid-alertdialogandroid-textinputlayout

Changing color of OK button in AlertDialog


I'm trying to change the color of the positive button in an AlertDialog in Android. Natively on my S8 the button is green, but when I try to change it, no matter what color I choose it turns up bright purple.(I've tried many different shades of blue and even pink to test)

I'm changing the color with:

dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = R.color.color_blue

I am calling it after the dialog.show().


Solution

  • I would rather use a theme to control the way your dialog looks like. In your case

    R.color.color_blue is a res id. You have to convert it to color. Eg

     dialog.getButton(DialogInterface.BUTTON_POSITIVE).textColor = ContextCompat.getColor(this, R.color.color_blue)