Setting buttons disabled text colour programatically

647 views Asked by At

I cant seem to get the text colour to change of the button from my dialog fragment. This button is generated programatically so I am unable to simply set the style in the xml.

This is what I tried:

dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setEnabled(false);
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.message_count_colour));

and this is my message_count_colour xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@android:color/white" />
    <item android:state_enabled="true" android:color="@android:color/white" />
</selector>

For some reason the disabled button stays the greyed out colour rather than white and I do not know why. Could someone help please

1

There are 1 answers

0
Ellie Zou On

You need to use getColorStateList().

Using btn.setTextColor(getResources().getColorStateList(R.color.message_count_colour)); this line of code can work.