android - apply selectableItemBackground in xml with support v7

AndroidAndroid XmlAndroid Support-Library

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCQMView Question on Stackoverflow
Solution 1 - AndroidVikramView Answer on Stackoverflow
Solution 2 - AndroidSunnyView Answer on Stackoverflow
Solution 3 - AndroidkraxorView Answer on Stackoverflow