Saturday, November 7, 2009

Setting defined colors for your Android UI components

I don't think the Android SDK manual is helpful enough when it comes to setting the colors that you defined in the XML file (in my example, res -> values -> color.xml). There is actually a workaround instead of following rigidly the guidelines to use the setTextColor(int color) method.

I believe you must have read the guidelines how to set the color of TextView from this URL and this URL and do not really feel like constructing another XML file as mentioned in the manual.

I also believe that you must have used this approach to set the color from the Java code,
btnInstance.setTextColor(R.color.YOUR_DEFINED_COLOR);
simply because the setTextColor asks for an Integer as the parameter but you don't see the changes taking place.

SOLUTION: this is THE workaround to set the color of your UI components from the codes.
btnInstance.setTextColor(getResources().getColor(R.color.YOUR_DEFINED_COLOR));

0 comments:

Post a Comment