5

I cant figure out how to get the current color of the text of a Button. I know its probably this but cant quite figure out the parameters.

public static int getTextColor (Context context, TypedArray attrs, int def)... 

Basically Im trying to do this

if(text is RED) {make text BLACK} else {make text RED} 

I know how to set the text color.

1 Answer 1

14

try this

ColorStateList mList = mButton.getTextColors(); int color = mList.getDefaultColor(); switch(color) { case Color.RED: mButton.setTextColor(Color.BLACK); break; case Color.BLACK: mButton.setTextColor(Color.RED); break; } 
Sign up to request clarification or add additional context in comments.

5 Comments

mButton.getTextColors().getDefaultColor() gives me -1
-1 refers to Color.WHITE ... mList.getDefaultColor() returns a constant value for a Color.
Why they have getCurrentTextColor() and getDefaultColor() both? Apparently they both does the same!
@66CLSjY how to get the integer for hex color?
@Reza, Can you elaborate?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.