12

I am working on an android app and using Spinner at many places in my app. What I want is to change the background color of the selected item of spinner, so that one can easily identify which item is currently selected.

I have already checked this link Setting background color for Spinner Item on selection but doing so will change the selected textview background color but do not change its color in dropdown list and I want to change the background color of the selected textview when I will see the dropdown list.

I want to change the color of selected item in list not on spinner, please see the image below.

enter image description here How can I do this? Please, can someone help me here?.

Thanks a lot in advanced.

Community
  • 1
  • 1
Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78
  • You need to implement it's getDropDownView() method, do it and if not able then post your code here – Vickyexpert Jul 29 '16 at 08:38
  • Apply onclick in adapter of spinner and set new background colour for particular item. – Bhunnu Baba Jul 29 '16 at 08:53
  • @Vickyexpert: Can you please give me an idea how can I use getDropDownView() method to achieve this. It would be a great help. Thanks a lot in advanced. – Prithniraj Nicyone Jul 29 '16 at 09:01
  • Show me your adapter class so I can edit it – Vickyexpert Jul 29 '16 at 09:08
  • @Vickyexpert: I am using simple ArrayAdapter without any customization in it. `ArrayAdapter _adapter = new ArrayAdapter<>(context, R.layout.simple_text_view); _adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); _adapter.add("One"); _adapter.add("Two"); _adapter.add("Three"); _spinner.setAdapter(_adapter);` Please check it once. – Prithniraj Nicyone Jul 29 '16 at 09:19

6 Answers6

27

You need to implement below method in your adapter class:

It will help you:

 int selectedItem = -1;

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {

   @Override
   public View getDropDownView(int position, View convertView, ViewGroup parent)
   {
       View v = null;
       v = super.getDropDownView(position, null, parent);
       // If this is the selected item position
       if (position == selectedItem) {
           v.setBackgroundColor(Color.BLUE);
       }
       else {
           // for other views
           v.setBackgroundColor(Color.WHITE);

       }
       return v;
   }
};

 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 mySpinner.setAdapter(dataAdapter);

Now on item selected in spinner put below

   selectedItem = position;
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • 1
    Thanks for this code :) But this is changing the background color of the 0th position item, not of the item which is selected in spinner. How can I do that and also the icon of dropdown is not visible after overriding the getDropDownView method. – Prithniraj Nicyone Jul 29 '16 at 09:40
  • you need to pass there selectedItemPosition instead of 0, – Vickyexpert Jul 29 '16 at 09:41
  • 1
    This works, but it's the old school solution. Can we have it with ripple and configured with styles instead? – Davideas Aug 27 '16 at 08:42
  • And finally set @Override public void onItemSelected(AdapterView> parent, View view, final int position, long id) { selectedItem = position; } – Teekam Apr 19 '17 at 11:39
  • If I want to change text color what I need to do? – DarckBlezzer Sep 05 '17 at 18:38
  • this solution is working for position given only first time.It is not changing color for every selection of spinner dropdown items – Erum Nov 03 '17 at 11:22
  • @Erum You have to set that position when you select any item from drop down list.. So on select item selected position will be changed and show you selected item in differ background – Vickyexpert Jan 17 '18 at 11:51
  • @DarckBlezzer you will get view inside the code check that view and then let's say you have taken text view then you can get it by view.findViewById and set it's text colour as per your requirement – Vickyexpert Jan 17 '18 at 11:53
2

Here is solution via XML:

Spinner looks like:

<Spinner
        android:id="@+id/settingsSleepingTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_main_button"
        android:popupBackground="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>

While creating spinner set setDropDownViewResource as custom layout:

adapter.setDropDownViewResource(R.layout.spinner_item);

And spinner_item.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner"
    android:textColor="#ffffff"
    android:textSize="20sp" /> 

And finally we set @drawable/spinner like this:

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

Hope my answer will be helpfull!

bene25
  • 580
  • 6
  • 18
0
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">@color/spinner_background</item>

</style>

Define Spinner_background color in color folder..

Apple Appala
  • 329
  • 4
  • 16
0

Try creating a selector in the drawable, something like,

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

And set the spinner background as

android:background="@drawable/spinner_selector"
LvN
  • 701
  • 1
  • 6
  • 22
  • This changes doesn't have any effect on background and it makes disappear the mini triangle on the right... – Davideas Aug 27 '16 at 08:01
0

I've searched the internet for a proper solution to do this without hardcoding the background behaviour in java code. You can achieve this (setting the selected item background color) using drawables.

What you need to do it set the dropdownViewResource to a custom layout. That layout should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner_item_background"
    android:gravity="left"
    android:padding="8dp" />

In spinner_item_background.xml, you can define a background for each item state. For example, if you want to have a ripple effect on press, but a solid effect when selected, you can try this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Activated state is the selected item -->
    <item android:state_activated="true" android:drawable="#00ff00"/>
    <!-- Pressed state is the one the user is interacting with -->
    <item android:state_pressed="true" android:drawable="#00ff00"/>
    <!-- The rest of the items -->
    <item android:drawable="#ffffff"/>
</selector>
  • I have tried this, but the states isn't working. I'm using: //ArrayAdapter adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.task_status, R.layout.spinner_item_when_closed); // adapter.setDropDownViewResource(R.layout.spinner_item_when_open); // And setting the background in file "res\layout\spinner_item_when_open.xml" using the code like the "spinner_item_background.xml" – educoutinho Oct 21 '17 at 03:46
  • I dont know if this will solve the problem, but the background-xml needs to be in the `drawable` folder. – Ted de Koning Oct 27 '17 at 07:37
  • You can't pass color code to android:drawable it give this error "incompatible with attribute drawable (attr) reference" – Bhojaviya Sagar Jun 04 '20 at 05:30
0

Create an int variable public static int posOfItemSpinnerSelected in your Activity:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        posOfItemSpinnerSelected=position;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

and in your adapter insert this code

if(position== YourActivity.posOfItemSpinnerSelected){
    textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.item_spinner_selected)); 
} else {
    textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.white));
} 

My spinner

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
truong luu
  • 392
  • 1
  • 14