I have some clickable views and I want to set the default available background that is present on list click (in ICS is a blue color). I have tried putting as background thi
There is a way to combine all the valid answers:
Define a attribute (eg. in values/attrs.xml):
<attr name="clickableItemBackground" format="reference"/>
In your platform dependent theme section (eg. in values/styles.xml or values/themes.xml) declare 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 for api-11+ (eg. in values-v11/styles.xml 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 use ?attr/clickableItemBackground
wherever needed.
In short - android:background="?selectableItemBackground"
;)
A solution similar to flx's answer, but without additional attribute definition.
Platform independent style used for pre-Holo devices (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 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>
It's list_selector_holo_dark
or the equivalent holo light version; and these are the defaults in Honeycomb and above. list_selector_background
is the non-holo version, used in Gingerbread and below.
EDIT: I believe (but can't confirm) that the platform agnostic selector is ?android:attr/listSelector
Alternatively,
android:background="?android:attr/listChoiceBackgroundIndicator
if you just want the items to highlight blue (or whatever the current default is) while they are clicked.
Setting Background
restricts you from choosing a background color or drawable later!
So my advice is to add this style to your styles.xml
:
<style name="TouchableView">
<item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>
And then on every 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 add onClickListener
to see the results.