Questioner
Questioner

Reputation: 2541

Can't change button text color in Android

I tried just setting android:textColor="#FFFFFF" and this didn't work so I checked other answers to the problem to find solutions.

I tried making a selector and then setting the android:textColor attribute to this selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
<item android:color="#ffffff" />
</selector>

That didn't work either.

I tried setting the text color programmatically: verifyButton.setTextColor(getApplication().getResources().getColor(R.color.white));

That didn't work either. The button text color is a dark grey and I can't seem to change it to white.

Upvotes: 1

Views: 1995

Answers (3)

Don't use getApplication(). Use getContext() instead. Or use ContextCompat.getColor() for backward compatibility

Upvotes: 0

卡尔斯路西法
卡尔斯路西法

Reputation: 113

Today, I also encountered this problem. I found that I set hint="AAA" for Button and then set textColor. This is invalid, it only works for text. I should set textColorHint to help those who have experienced this minor problem

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

You can try with

setTextColor(Color.parseColor("#FFFFFF"));

FYI

Why calling getApplication() ?

getApplication() is only available on the Activity class and in the Service class, whereas getApplicationContext() is declared in the Context class.

  • You should call getApplicationContext() instead of getApplication() .

    setTextColor(getApplicationContext().getResources().getColor(R.color.white));

You should call getApplicationContext() or Direct Activity Class .

Return the context of the single, global Application object of the current process.

getApplication() vs. getApplicationContext()

Upvotes: 2

Related Questions