80

even with android support v7 included in my application

adding android:background="?android:attr/selectableItemBackground"

makes my IDE, Eclipse throw an error (preventing me from compiling), notifying me that selectableItemBackground is only for min Api 11 and up.

How do I add this attribute to a background in XML?

assume that copying and pasting from a higher library is not a solution

CQM
  • 42,592
  • 75
  • 224
  • 366

3 Answers3

226

Since the attribute is defined in a library (support v7), you would use it as a user-defined attribute: i.e without the android: prefix:

android:background="?attr/selectableItemBackground"

The error you see is pointing out that ?android:attr/selectableItemBackground is available for API versions >= 11. True, indeed.

CQM
  • 42,592
  • 75
  • 224
  • 366
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • 2
    1) How do you know that attribute is in support v7 and not in v4 ? (I'm downloading rev19 of the support lib at the moment) 2) do you have an example of the URI using a user-defined attribute ? – Someone Somewhere Jan 03 '14 at 23:03
  • 2
    @SomeoneSomewhere From what I can tell, v4 does not define any attributes. I know that `selectableItemBackground` is defined in v7 by looking at [android.support.v7.appcompat.R.attr](http://developer.android.com/reference/android/support/v7/appcompat/R.attr.html). About your second question, are you asking how a user-defined attribute works? – Vikram Jan 08 '14 at 18:58
  • I've actually forgotten what I was doing at the time when I posed Question #2. I believe the following link answered that question, especially the comment: http://stackoverflow.com/a/11388952/550471 – Someone Somewhere Jan 08 '14 at 20:10
  • 14
    @SomeoneSomewhere Anything that's being provided by Android system is accessed through the `android:` namespace prefix. This does not include support libraries as they are add-ons. Attributes are defined in `attrs.xml`, and set in `themes.xml` and/or `styles.xml`. So, if you were to assign your own drawable to `selectableItemBackground`, you wouldn't use the `android:` namespace. But you will, if the drawable is being provided by Android system. – Vikram Jan 27 '14 at 17:07
  • @Vikram how do we set the background to "attr/selectableItemBackground" with java? – Prakhar Jan 06 '15 at 19:14
  • @Prakhar I answered this elsewhere: [?android:attr/selectableItemBackground with another existing background](http://stackoverflow.com/questions/20269910/androidattr-selectableitembackground-with-another-existing-background). The question _is_ different, but you only need the first part of my answer. – Vikram Jan 28 '15 at 18:19
  • @Vikram can you explain why applying this attribute to ImageView highlight it with a blue color, but my application theme is AppCompat and I expect a grey color for highlight cause according to SDK/appcompat this attribute must use grey drawable. – Raman Branavitski Jan 25 '16 at 10:16
17

Here is selectedItemBackground. You can find it in /platforms/android-14/data/res/themes.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_selector_background_focused" />
    <item android:drawable="@color/transparent" />

</selector>

and you can find drawables in your Android SDK directory

../platforms/android-14/data
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • 1
    so you are suggesting I copy it to a theme or my own drawable instead of wondering how to get it out of the support library that should be working with my older api level? – CQM Oct 31 '13 at 19:26
  • You can set minimum api greater than 11 otherwise you have to copy drawables from there in your `res` folder. – Sunny Oct 31 '13 at 19:31
  • I feel like I should be able to set a namespace or something to get to the support library attributes – CQM Oct 31 '13 at 19:31
  • You can use HoloEveryWhere library then it will work from API 7+. – Sunny Oct 31 '13 at 19:33
  • I use android support library v7, it serves the same purpose as holo everywhere, lets assume the support library v7 is my only tool, how do I get the same result? – CQM Oct 31 '13 at 19:35
  • are you sure that these resource are available in v7? – Sunny Oct 31 '13 at 20:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40341/discussion-between-sunny-and-cqm) – Sunny Oct 31 '13 at 20:11
  • its in here https://chromium.googlesource.com/android_tools/+/881586ca84f2fb8e82faa9c8d645416d175d0f01/sdk/extras/android/support/v7/appcompat/res/values/attrs.xml as well as the v7 mediarouter library – CQM Nov 01 '13 at 14:33
4

Not an expert on the subject, but it seems you need platform version based theming. The official guide explains this process pretty well I think.

You have to create different XML files for each version and save them in res/values-v7, res/values-v11 etc. Then use those styles for your views. Something like this:

in res/values-v7:

<style name="LightThemeSelector" parent="android:Theme.Light">
    ...
</style>

in res/values-v11:

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
    <item name="selectableItemBackground">?android:attr/selectableItemBackground</item>
    ...
</style>

Then use style for the view:

<TextView
    style="@style/LightThemeSelector"
    android:text="@string/hello" />

Hope this helps. Cheers.

kraxor
  • 649
  • 8
  • 16
  • okay this element "?android:attr/selectableItemBackground" is already in the android support v7 library. I shouldn't need to copy and themes from other libraries or make platform specific code – CQM Oct 31 '13 at 19:44
  • 2
    Actually kraxor makes a good point about customizing the background for different API levels. For example API 21 has a ripple effect not supported before 21 (even with AppCompat) and so you may want to style the item background differently – Greg Ennis Mar 06 '15 at 03:34
  • 3
    this is a good solution, since it can be reused on other views. however, in my experience, the attribute in the style definition should be 'android:background' instead of 'selectableItemBackground', like so: `` – kip2 Jun 10 '15 at 14:07