Search code examples
javaandroidspinnerandroid-spinner

How to Change the background color of Spinner selection in Android


enter image description here

In the above picture GAIN 3 is selected but its not visible properly , so how can i change that color to darker color. basically i want to change the selected text background in darker color.

I'm using com.jaredrummler.materialspinner.MaterialSpinner Spinner.

Here's the java implementation.

spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

    @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
        text = spinner.getText().toString();
        Log.e("Spinner Listener",text);

        if(text.contains("GAIN 0")){
            sendToDevice("F");
        } else if(text.contains("GAIN 1")){
            sendToDevice("G");
        } else if(text.contains("GAIN 2")){
            sendToDevice("H");
        } else if(text.contains("GAIN 3")){
            sendToDevice("I");
        }
    }
});

And the layout item looks like the following.

<com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    app:ms_dropdown_max_height="350dp"
    app:ms_dropdown_height="wrap_content"
    android:textColorHighlight="#000000"
    android:layout_width="130dp"
    style="@style/spinner_style"
    android:popupTheme="@android:style/ThemeOverlay.Material"
    android:textColor="@color/blue"
    android:layout_below="@+id/testmodetitle"
    android:layout_height="wrap_content"
    android:layout_marginTop="55dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toEndOf="@+id/button1"
    android:layout_marginStart="30dp" />

Solution

  • To change background color and other color this library has provided some attributes. To change background color of selected item use below code.

    <com.jaredrummler.materialspinner.MaterialSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:ms_background_selector="@drawable/selector_gray_white_spinner"
            app:ms_dropdown_height="wrap_content"
            app:ms_dropdown_max_height="350dp" />
    

    create one selector in drawable having name selector_gray_white_spinner.xml

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

    Add dark color in your color.xml file

    <color name="darkGray">#acacac</color>