Default selector background in Clickable Views

AndroidCss SelectorsDrawable

Android Problem Overview


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 this:

android:background="@android:drawable/list_selector_background"

but it is not the default blue I have by default (the one I used is orange). What is the drawable used by default on android as click selector?

Thanks

Android Solutions


Solution 1 - Android

This works for me:

android:background="?android:attr/selectableItemBackground"

Solution 2 - Android

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

Solution 3 - Android

If you are using appcompat-v7, you can just use ?attr/selectableItemBackground.

Solution 4 - Android

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.

Solution 5 - Android

This works fine on api 11 and above. But as noted it wont work on previous versions.

android:background="?android:attr/selectableItemBackground"

Here is a solution to have it run on all versions running android.

  1. Add the appropriate colors within the colors.xml which is located within your values folder. It should appear as such:

     <color name="white">#ffffff</color>
     <color name="blue">#7ecce8</color>
    
  2. Create 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> 
    
  3. Go to your view or button and set the newly created button_selection.xml as its background.

     android:background="@drawable/button_selection"
    

Solution 6 - Android

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>

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

In short - android:background="?selectableItemBackground" ;)

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
QuestionMatroskaView Question on Stackoverflow
Solution 1 - AndroidBogdan BalanView Answer on Stackoverflow
Solution 2 - AndroidAlex CurranView Answer on Stackoverflow
Solution 3 - AndroididunnololzView Answer on Stackoverflow
Solution 4 - AndroidflxView Answer on Stackoverflow
Solution 5 - AndroidChallengeAcceptedView Answer on Stackoverflow
Solution 6 - AndroidAleksejs MjaliksView Answer on Stackoverflow
Solution 7 - AndroidColinView Answer on Stackoverflow
Solution 8 - AndroidMorteza RastgooView Answer on Stackoverflow
Solution 9 - AndroidAnoop M MaddasseriView Answer on Stackoverflow