1

I want to change my spinner item background (not the spinner background) when the item is selected.

I want it like this:

Image

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
Chintak Patel
  • 748
  • 6
  • 24

1 Answers1

1

First create a drawable under drawable folder named

my_click_changer

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

EDIT this is for changing the text color

create another drawable

text_color_changer

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

then create a individual layout for each Spinner item,

your_xml_name.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   style="?android:attr/spinnerItemStyle"
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:background="@drawable/my_click_changer"
   android:ellipsize="marquee"
   android:gravity="center"
   android:padding="10dp"
   android:singleLine="true"
   android:textAlignment="inherit"
   android:textSize="15sp"
   android:textColor="@drawable/text_color_changer"
   android:textStyle="bold" />

And in the code create Adapter by

    final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(), R.layout.your_xml_name, yourList);
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
  • 1
    Thank you, I added one more thing, `` and this worked perfectly. but now as my font is white too, I want when state is activated, my text should be black colored.. so what should I do ? – Chintak Patel Jul 18 '16 at 10:41
  • @ChintakPatel i have taken this into account.. And please view the **Edit** portion of my answer.. I have already answered to it.. And use you appropirate `colors` too – Sanoop Surendran Jul 18 '16 at 10:45