16

I'm working on an app right now which uses subclassed ImageView to show an image with a bounding box over part of it.

Right now the box is drawn in yellow no matter what, but I think it would look a lot better if the color matched the system's button pressed color, such as orange for the Droid, green for the Evo, or blue for the Galaxy S.

I looked around the APIs a bit, but couldn't find how to programmatically nab that color. Any ideas?

Macarse
  • 91,829
  • 44
  • 175
  • 230
infinitypanda
  • 398
  • 1
  • 2
  • 11
  • this is a good question and i'm afraid there is no solution. really sad. – mad Jan 02 '11 at 18:48
  • It's been 5 years since this question was asked... the current answer is now located here: http://stackoverflow.com/questions/12375766/how-to-get-background-color-from-current-theme-programmatically (that first answer definitely works...) – pbristow Feb 12 '16 at 17:17

3 Answers3

8

From Android ICE_CREAM_SANDWICH (API 14), you have the theme colors in android.R.color.*

For example;

myTextView.setTextColor(getResources().getColor(android.R.color.holo_red_light));
Bridge
  • 29,818
  • 9
  • 60
  • 82
Sebastian Tomac
  • 101
  • 1
  • 4
  • 1
    In my limited understanding, something available from android.R.color is a static color and not a changeable color from the app's Theme (changable at runtime). The answer for doing that is here: http://stackoverflow.com/questions/12375766/how-to-get-background-color-from-current-theme-programmatically – pbristow Feb 12 '16 at 17:16
8

You can look at the source from android's themes.xml, styles.xml, and colors.xml. The one thing you notice from the colors.xml is that there isn't very many colors defined. This is because most of the widgets are done via 9-patch files.

Button style:

 223     <style name="Widget.Button">
 224         <item name="android:background">@android:drawable/btn_default</item>
 225         <item name="android:focusable">true</item>
 226         <item name="android:clickable">true</item>
 227         <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
 228         <item name="android:textColor">@android:color/primary_text_light</item>
 229         <item name="android:gravity">center_vertical|center_horizontal</item>
 230     </style>

All of the work done to change the background colors is done in the btn_default Drawable.

Source of btn_default.xml:

  17 <selector xmlns:android="http://schemas.android.com/apk/res/android">
  18     <item android:state_window_focused="false" android:state_enabled="true"
  19         android:drawable="@drawable/btn_default_normal" />
  20     <item android:state_window_focused="false" android:state_enabled="false"
  21         android:drawable="@drawable/btn_default_normal_disable" />
  22     <item android:state_pressed="true" 
  23         android:drawable="@drawable/btn_default_pressed" />
  24     <item android:state_focused="true" android:state_enabled="true"
  25         android:drawable="@drawable/btn_default_selected" />
  26     <item android:state_enabled="true"
  27         android:drawable="@drawable/btn_default_normal" />
  28     <item android:state_focused="true"
  29         android:drawable="@drawable/btn_default_normal_disable_focused" />
  30     <item
  31          android:drawable="@drawable/btn_default_normal_disable" />
  32 </selector>

Each one of those is a 9-patch file. The problem is that those are pngs. The colors are built into the image files and are not defined anywhere. As you've noticed these images can be replaced and the look changes.

Unfortunately, what you want is not possible. You are going to have to choose a single color to go with. This color probably should be chosen to fit with the rest of your application. Sorry :(

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
1

Check this link: Using Platform Styles and Themes

I don't know if subclassing is the best thing to do in this case. I would try:

  • Use ImageButton instead of subclassing.
  • Add the platform style to the button.
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • 2
    I read that, but it wasn't helpful. It tells me how to set themes, but I want to get themes. And ImageButton isn't what I'm looking for, sorry. The picture doesn't need to be clickable, I just want to use the same color as a button. – infinitypanda Jul 30 '10 at 22:04