简体   繁体   中英

How to Change the background color of Spinner selection in Android

在此输入图像描述

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" />

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>

There are some attributes available along with the implementation of that specific library. Please have a look in the readme.md section where the attributes are listed.

在此输入图像描述

I think you might consider using ms_background_selector attribute in your layout where you have declared the spinner.

So the layout declaration will look like this.

<com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    app:ms_background_selector="@drawable/your_darker_selector"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Add a file named your_darker_selector.xml and put the following code inside the file.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@android:color/darker_gray"/>
    <item android:state_checked="false" android:drawable="@android:color/white" />
</selector>

Modify the color from the selector file as per your necessity.

Use this way it will help you:

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;
}
};

为第一个微调器项提供html颜色代码。

String styledText = "This is <font color='red'>simple</font>."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM