[ACCEPTED]-Default selector background in Clickable Views-drawable
This works for me:
android:background="?android:attr/selectableItemBackground"
0
It's list_selector_holo_dark
or the equivalent holo light version; and 4 these are the defaults in Honeycomb and 3 above. list_selector_background
is the non-holo version, used in 2 Gingerbread and below.
EDIT: I believe (but can't confirm) that 1 the platform agnostic selector is ?android:attr/listSelector
If you are using appcompat-v7
, you can just use ?attr/selectableItemBackground
.
0
There is a way to combine all the valid 7 answers:
Define a attribute (eg. in values/attrs.xml):
<attr name="clickableItemBackground" format="reference"/>
In 6 your platform dependent theme section (eg. in 5 values/styles.xml or values/themes.xml) declare 4 this:
<style name="Theme.Platform" parent="@android:style/Theme.Whatever">
<item name="clickableItemBackground">@android:drawable/list_selector_background</item>
</style>
In your platform dependent theme section 3 for api-11+ (eg. in values-v11/styles.xml 2 or values-v11/themes.xml) declare this:
<style name="Theme.Platform" parent="@android:style/Theme.Holo.Whatever">
<item name="clickableItemBackground">?android:attr/selectableItemBackground</item>
</style>
Then 1 use ?attr/clickableItemBackground
wherever needed.
A solution similar to flx's answer, but 4 without additional attribute definition.
Platform 3 independent style used for pre-Holo devices 2 (in res\values\styles.xml
):
<style name="SelectableItem">
<item name="android:background">@android:drawable/list_selector_background</item>
</style>
Style for Holo devices (API Level 1 14+) (in res\values-v14\styles.xml
):
<style name="SelectableItem">
<item name="android:background">?android:attr/selectableItemBackground</item>
</style>
Apply style to needed view, e.g., LinearLayout
:
<LinearLayout
style="@style/SelectableItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</LinearLayout>
This works fine on api 11 and above. But 8 as noted it wont work on previous versions.
android:background="?android:attr/selectableItemBackground"
Here 7 is a solution to have it run on all versions running 6 android.
Add the appropriate colors within 5 the colors.xml which is located within your 4 values folder. It should appear as such:
<color name="white">#ffffff</color> <color name="blue">#7ecce8</color>
Create 3 an xml selector file. Here I named it button_selection.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/blue"/> <!--pressed --> <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused --> <item android:drawable="@color/white"/> <!-- default --> </selector>
Go 2 to your view or button and set the newly 1 created button_selection.xml as its background.
android:background="@drawable/button_selection"
Alternatively,
android:background="?android:attr/listChoiceBackgroundIndicator
if you just want the items 2 to highlight blue (or whatever the current 1 default is) while they are clicked.
Setting Background
restricts you from choosing a background 4 color or drawable later!
So my advice is 3 to add this style to your styles.xml
:
<style name="TouchableView">
<item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>
And then on every 2 view you only need to add:
android:theme="@style/TouchableView"
Like ANY VIEW!
<TextView
android:id="@+id/my_orders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:theme="@style/TouchableView"
android:background="@color/white"
android:gravity="left"
android:padding="10dp"
android:text="@string/my_orders"
android:textColor="@color/gray"
android:textSize="14sp" />
Don't forget to 1 add onClickListener
to see the results.
In short - android:background="?selectableItemBackground"
;)
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.