android - apply selectableItemBackground in xml with support v7
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
Solution 1:
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:
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:
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.