Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get theme colors

Tags:

android

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?

like image 385
infinitypanda Avatar asked Jul 30 '10 20:07

infinitypanda


3 Answers

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 :(

like image 196
Rich Schuler Avatar answered Oct 31 '22 13:10

Rich Schuler


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));
like image 8
Sebastian Tomac Avatar answered Oct 31 '22 12:10

Sebastian Tomac


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.
like image 1
Macarse Avatar answered Oct 31 '22 12:10

Macarse