Qwertie
Qwertie

Reputation: 17216

How to set Android button background color?

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.

Upvotes: 2

Views: 25619

Answers (4)

Yar
Yar

Reputation: 4561

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"
   />        

Upvotes: 2

Qwertie
Qwertie

Reputation: 17216

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.

Upvotes: 3

FoamyGuy
FoamyGuy

Reputation: 46856

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

Upvotes: 0

Christine
Christine

Reputation: 5575

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

Upvotes: 1

Related Questions