tags:

views:

116

answers:

3

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?

+1  A: 

You should get such things in system properties.

public static Properties System.getProperties()

It returns a hashtable of all properties. Iterate through it to find appropriate.

Ankit Jain
How will I know when I've hit the right property? Is there a handle or name or something that I can use?
infinitypanda
you got to print all key-value pairs!
Ankit Jain
I printed it. There is nothing in there pertaining to color. It's all information about the setup of the app and the DVM.
infinitypanda
+1  A: 

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

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"&gt;
  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 :(

Qberticus
Well that's depressing. Thanks though.
infinitypanda