Saturday, November 27, 2010

[android-developers] Setting color of drawable shape in code

I am using a shape defined as a drawable as background for a TextView.
This allows me to add rounded corners and other other effects.
The shape is defined like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="8dp" />
</shape>

and I am using it like this:

<TextView
android:id="@+id/project"
style="@style/textView"
android:background="@drawable/project_textview_background"
/>

Now, I need to change the color of that TextView programmatically
depending on some conditions. The conditions are note related to
button states like focuses, selected, etc... It depends on the data.
My current workaround was to create a 9-patch image for each color but
I don't like this solution because there are 12 colors and I want to
use them in other places with different shapes. I'll end up with
dozens of 9-patch images for something that could be done in code.

- I tried to do setBackgroundColor but that seems to overwrite the
background I previously defined so it doesn't show the rounded corners
anymore.
- I looked at a bunch of other API methods but got nowhere

Any help would be very much appreciated. Thank you
Any ideas?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate