Those aren't colors. They are a few nine-patch images out of a StateListDrawable
. I am skeptical that there will be a reliable way for you to determine what the color is, one that will work across all devices and all versions of Android.
This is pretty much a duplicate of: http://stackoverflow.com/questions/2038040/android-listview-selector-color
Also, why do you need to detect the colours? Just do nothing and your widgets will fit in to the platform's existing look & feel.
Or if you're writing a custom theme, just make yours inherit from android:Theme
.
I have this same exact problem, and I considered the solution proposed by the OP. However, I do not believe that the solution presented is ideal either. Even though you're still using the default images for focused and pressed state, the normal state is hardcoded into your app and you cannot guarantee that the future versions of Android or Sense will not change the look of the default button. This means we're still stuck.
Instead of changing the actual graphic, I've been exploring drawable's setColorFilter, which works perfectly in coloring the normal unfocused/unpressed button. However, I can't find a way to ONLY apply it to the normal state, since the filter+orange of the focus/pressed state look really nasty.
Any ideas how to apply a color filter to only 1 state of a default drawable?? Like maybe applying a color filter to an XML similar to what the OP did? This would be the most ideal solution if we could achieve this!
See more details on my post in google groups: http://groups.google.com/group/android-developers/browse_thread/thread/1b74ee5f31ce2a1c#
I had this problem too. As already stated, the problem is that the backgrounds aren't simple colors, they're Drawables that could take on all kinds of appearances. However, I found a work-around that may help. If your custom component looks something like an existing one, e.g. a Button or ListView entry, you can just steal their background/selector and set that as the background for your custom component. E.g., in your custom component constructor:
setBackgroundDrawable(new Button(context).getBackground());
or for a background more suitable for list-like components:
setBackgroundDrawable(new ListView(context).getSelector());
You may want to optimise that code somewhat, but you get the idea.