Change background color of the selected item in An

2020-06-16 03:42发布

问题:

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.

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

Thanks a lot in advanced.

回答1:

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;


回答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!



回答3:

<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..



回答4:

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"


回答5:

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>


回答6:

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