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

3 Answers 3

226
+50

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.

7
  • 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 ? Jan 3, 2014 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. About your second question, are you asking how a user-defined attribute works?
    – Vikram
    Jan 8, 2014 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: stackoverflow.com/a/11388952/550471 Jan 8, 2014 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, 2014 at 17:07
  • @Vikram how do we set the background to "attr/selectableItemBackground" with java?
    – Prakhar
    Jan 6, 2015 at 19:14
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
8
  • 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, 2013 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, 2013 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, 2013 at 19:31
  • You can use HoloEveryWhere library then it will work from API 7+.
    – Sunny
    Oct 31, 2013 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, 2013 at 19:35
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.

3
  • 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, 2013 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 6, 2015 at 3: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: <style name="LightThemeSelector" parent="android:Theme.Holo.Light"> <item name="android:background">?android:attr/selectableItemBackground</item> ... </style>
    – kip2
    Jun 10, 2015 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.