2

I would like to use a custom background color for a couple of buttons. I tried android:background="#cc2" but it replaces the entire button, so it doesn't look like a button anymore.

Evidently one can use a <selector> (drawable) for this. Is this the only way? Where do I get a template for the original, standard Android button?

Note: the accepted answer to this question isn't what I want, since it doesn't look like a standard Android button.

Community
  • 1
  • 1
Qwertie
  • 16,354
  • 20
  • 105
  • 148
  • There are ways to apply a color filter to the button, like the one from the mentioned by you post: `button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));`. Maybe this is what you need since the button remains "standard Android button"? – Vit Khudenko Jun 13 '12 at 21:32

4 Answers4

3

Based on this question I was able to determine that the standard button template for e.g. Android 2.2 is located at

C:\Program Files (x86)\Android-sdk\platforms\android-8\data\res\drawable\btn_default.xml

Obviously, the path may be different on your PC. It refers to drawables such as "@drawable/btn_default_normal" which refer to 9-patch files, e.g. the HDPI ones are

data\res\drawable-hdpi\btn_default_normal.9.png
data\res\drawable-hdpi\btn_default_normal_disable.9.png
data\res\drawable-hdpi\btn_default_pressed.9.png
data\res\drawable-hdpi\btn_default_selected.9.png
data\res\drawable-hdpi\btn_default_normal_disable_focused.9.png

How to find resources: I started from Button.java's constructor (use SDK Manager to download Sources for Android SDK):

this(context, attrs, com.android.internal.R.attr.buttonStyle);

Which refers to an <item> in res/values/themes.xml:

<item name="buttonStyle">@android:style/Widget.Button</item>

Which refers to a <style> in res/values/styles.xml:

<style name="Widget.Button">
    <item name="android:background">@android:drawable/btn_default</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
    <item name="android:textColor">@android:color/primary_text_light</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
</style>

Which finally refers to res/drawable/btn_default.xml.

Community
  • 1
  • 1
Qwertie
  • 16,354
  • 20
  • 105
  • 148
2

Add your style

 <style name="MyButtonStyle" parent="android:Widget.Button">
        <item name="android:background">@drawable/btn_default</item>
 </style>  

and your selector (here btn_default.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_pressed" />
    <item
         android:drawable="@drawable/btn_default_normal" />
  </selector>

and your images (9 patch) btn_default_normal and btn_default_pressed (the names can be different than these) Then, apply the style to your button in xml:

  <Button
      ...
      style="@style/MyButtonStyle"
   />        
Yar
  • 4,543
  • 2
  • 35
  • 42
1

If you change the button background, the entire background is changed, as you noticed. If you want to have a button in your own style or color, you can create a 9-patch file with the graphic of your choice as the button background. If you want it to show selection or click states, you can use a state list with a selector.

Both 9patch and selectors are explained here: http://developer.android.com/guide/topics/resources/drawable-resource.html

Christine
  • 5,617
  • 4
  • 38
  • 61
0

Where do I get a template for the original, standard Android button?

The system resources are included in the SDK.

you can find them (on windows) at: sdk-root/platforms/[pick a platform]/data/res/drawable/

depending on which platform level you are looking for you may find that the xml selectors you need are in plain drawable, and the png's themselves are in one of the resolution specific folders e.g. drawable-mdpi or drawable-hdpi

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thanks but so far I have not been able to identify the actual drawable, whether it is a selector or 9-patch or both. – Qwertie Jun 13 '12 at 22:19