Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text style based on state with XML?

StateListDrawables and <selector> are great for setting different drawables for each state of a view, but is there a way to tie in what text style that view should use for each of those states?

For example, if you had a button that was white with black text, and when clicked, the button color became black, you would want your text color to change to white. I can imagine how I would do this with code, but is there a way to do it with XML similar to the <selector> used for drawables?

like image 688
cottonBallPaws Avatar asked Feb 26 '23 00:02

cottonBallPaws


1 Answers

Well there sort of is. I don't believe there is a way to say, something like state_focused use Bold or italics or fontSize = 20. But there are ways to change the color. So using the selector mechanic you can create a ColorStateList

http://developer.android.com/reference/android/content/res/ColorStateList.html

Basically you do the same thing as you would a state list drawable using a selector except that you can place a color inside the individual items. Then you save your XML inside the color folder.

/res/values/color/my_stateful_color.xml

and set the android:textColor to "@color/my_stateful_color"

like image 141
Greg Giacovelli Avatar answered Mar 05 '23 09:03

Greg Giacovelli