3

Spinner xml:

<Spinner
        android:id="@+id/sort_by_spinner"
        android:layout_marginLeft="40dip"
        android:layout_marginRight="40dip"
        android:layout_marginBottom="10dip"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/title_bar"
        android:drawSelectorOnTop="true"
        android:dropDownSelector="@drawable/spinner_selector"
    />

I've tried using android:background=... buy itself, with dropDownSelector, with and without listSelector=...; with and without listItemDropDownSelector=... and all permutations with drawSelectorOnTop

spinner_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/app_tint"/>
        </shape>
    </item>
    <item 
        android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/app_tint"/>
        </shape>
    </item>
</selector>

I always get the default orange color. I've read numerous posts on this; just can't get it to happen. I have to support v10 and up. What's missing?

flx
  • 14,146
  • 11
  • 55
  • 70
wkhatch
  • 2,664
  • 7
  • 36
  • 45
  • android:dropDownSelector="@drawable/spinner_selector i do not think it is working properly for you..please visit http://stephenpengilley.blogspot.in/2013/01/android-custom-spinner-tutorial.html that you will know how to do this – Jitesh Upadhyay Feb 25 '14 at 03:33
  • the same selector works elsewhere no problem, so I don't think that's it. I think something with the attribute I'm setting on the Spinner itself. I think, anyway. I also tried the @flx answer; does't work either – wkhatch Feb 25 '14 at 04:06

2 Answers2

2

try it as:

<Spinner         
    android:id="@+id/sort_by_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="@dimen/space"
    android:background="@drawable/selection_normal"
    android:dropDownSelector="@drawable/list_item_selector"
    android:spinnerMode="dropdown" />

and the list_item_selector as

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="@color/app_tint"></item>
    <item android:state_pressed="true" android:drawable="@color/app_tint"></item>
    <item android:drawable="@color/white"></item>
</selector>

selection_normal is any image for the spinner, you can ignore it if not needed.

[EDIT]

In order to set highlight the list row of the item touched / clicked do the following:

in the getDropDownView instead of getView method in the spinner's adapter set the selector using the code:

view.setBackgroundResource(R.drawable.list_item_selector)

or using the setBackground method of the view.

  • are you saying I can ignore the background property? Or, just assign any drawable to it? – wkhatch Feb 25 '14 at 04:54
  • you can assign and drawable to it :) – Rat-a-tat-a-tat Ratatouille Feb 25 '14 at 04:55
  • that impacts the selected state of the spinner, and does work, but I'm looking for the selected state of the list items inside it. Maybe that's my problem here... – wkhatch Feb 25 '14 at 05:01
  • you could then maintain a list of items selected or the item selected, and in the getDropDownView instead of getView method in the spinner's adapter set the selector. – Rat-a-tat-a-tat Ratatouille Feb 25 '14 at 05:02
  • Let me clarify, just to make sure I'm explaining what I'm trying to accomplish properly. I just need the row to highlight a different color on touch/click. The list is single select only; I don't need to maintain the actual selection state; sorry for the confusion – wkhatch Feb 25 '14 at 05:07
  • I am indeed confused :(, and the selector doesnt accomplish what you want is it? true, a spinner is single select only – Rat-a-tat-a-tat Ratatouille Feb 25 '14 at 05:10
  • The selector works, but for the spinner itself, not the touched item within it's list. I just need to change the highlighted color of the items when the user touches them. So, following along with your suggestion, if I over ride getDropDownView, getting the current view for row, I'd then set it's ??? property to my selector? Closer? – wkhatch Feb 25 '14 at 05:21
  • which property of the view am I setting the selector drawable on? – wkhatch Feb 25 '14 at 05:26
  • and that works great; update your answer and victory is yours! lol. Thanks; I was really pulling my hair out on this one. – wkhatch Feb 25 '14 at 05:29
0
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"   
  android:drawable="@drawable/audio_selector"/>    
 <item android:drawable="@drawable/launcher" />
</selector>

easy to use selector place this file in drawable folder you also have image in drawable folder

flx
  • 14,146
  • 11
  • 55
  • 70
Muhammad Usman Ghani
  • 1,279
  • 13
  • 19