3

I'd like to change the background for an image button. Basically the image I have looks great on a transparent background and a bit lousy when there are a bunch of them all with a non-transparent background.

This should be easy - just change the android:background on the button to a transparent color (via a drawable): click_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true"><color android:color="#FF008080" />
    </item>
    <item android:state_pressed="true"><color android:color="#FF008080" />
    </item>
    <item><color android:color="#00000000" />
    </item>

</selector>

Then the button

<ImageButton
                android:id="@+id/players"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:background="@drawable/click_background"
                android:contentDescription="@string/players"
                android:src="@drawable/speaker_tile" />

The problem is that I actually want to retain the state_focussed and state_pressed versions of the background (which I presume is derived from the theme). I would like to have the background appear when the button is pressed or selected (and appear with the exact same colour/drawable) that would be used by the theme normally when a button is pressed would be helpful.

I was wondering if there is a way to do one of the following, but have so far been unable to find anything on either:

  1. Define a selector that in some what inherits from another (similar to the way a theme can inherit from another).

  2. Create an entirely new selector but have it's XML reference the color/drawable of the theme's state_focussed and state_pressed used for an image button. Edit: It looks like this option is out. I would have needed attributes, but you can't reference them from a drawable. See here

I'd like to do this in Declarative XML rather than Programmatic Java if possible.

Community
  • 1
  • 1
Philip Couling
  • 13,581
  • 5
  • 53
  • 85

1 Answers1

3

You can copy the drawables used by the Android OS into your project and use them in your state-list drawable. You can find the images in {android-sdk-directory}/platforms/android-##/data/res/drawable[-*dpi]

EDIT: If you go to {android-sdk-directory}/platforms/android-##/data/res/values, you'll find the themes.xml and styles.xml files used by Android. Using them you can figure out which drawables you'll be looking for.

For example, the default theme on newer versions of Android is Theme.Holo. This theme has a default style for ImageButtons declared like so:

<item name="imageButtonStyle">@android:style/Widget.Holo.ImageButton</item>

In styles.xml, this style is defined as follows:

<style name="Widget.Holo.ImageButton" parent="Widget.ImageButton">
    <item name="android:background">@android:drawable/btn_default_holo_dark</item>
</style>

Thankfully the background attribute is right there defined in plain sight. Sometimes it's inherited from a parent style and you have to find that instead. In any case, here's the drawable (found in the /drawable directory since it's 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_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
    android:drawable="@drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
    android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
    android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
<item
     android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>

So those are the drawables Android uses for the background of a standard ImageButton in the default (holo dark) theme.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Thanks, Any hints on which item in the drawable directory I'll be looking for?. There are rather a lot to search through. – Philip Couling Apr 01 '13 at 10:14
  • I think ImageButton uses the same backgrounds as regular Button. It also depends on what platform level. On newer versions of Android, I think btn_default_{state}_holo_dark.9.png and btn_default_{state}_holo_light.9.png are the ones you are looking for, but I'm not positive. Usually I have to look through res/values/themes.xml and res/values/styles.xml in the platform resources to figure out exactly what I'm looking for – Karakuri Apr 01 '13 at 16:12
  • Dunno on the down vote. I traced this (with your help) to btn_default_holo_dark.xml and its supporting images. Would be grateful if you could add that to your answer. – Philip Couling Apr 01 '13 at 16:54