(2) Create an Android xml file in the new 'color' folder and name it 'mycolorlist.xml' or another name of your choosing. Place the following markup in your new xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="@color/mycolor1"
android:state_pressed="false" />
<item
android:color="@color/mycolor2"
android:state_pressed="true" />
</selector>
Be sure that 'mycolor1' and 'mycolor2' are colours specified in your 'res/values/colors.xml' file. If you're not sure how to specify colors in your 'colors.xml' file, see the end of this post.
(3) Add the following code to the TextView to which you wish to apply the new ColorStateList...
myTextView.setTextColor(getResources().getColorStateList(R.color.mycolorlist));
... where myTextView is a TextView instance.
Done! Now when myTextView is pressed, its colour will be 'mycolor2', and otherwise its colour will be 'mycolor1'.
Lastly, here is an example of what your 'colors.xml' file might look like...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="mycolor1">#ff005eff</color>
<color name="mycolor2">#ff86C4ed</color>
</resources>
... which specifies two different shades of blue.
4 comments:
Hey thanks it worked :)
No prob. Glad I could be of assistance :)
great tutorial. i need it function, because when i just settextcolor, the color is going to black.
Thanks for the post. Its working perfectly.
Post a Comment