Change Background Color Of The Selected Item In Android Spinner
Solution 1:
You need to implement below method in your adapter class:
It will help you:
intselectedItem= -1;
ArrayAdapter<String> dataAdapter = newArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {
@Overridepublic View getDropDownView(int position, View convertView, ViewGroup parent)
{
Viewv=null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item positionif (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;
Solution 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"?><TextViewxmlns: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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/colorPrimaryLight"android:state_hovered="true" /><itemandroid:drawable="@color/colorPrimaryLight"android:state_selected="true" /></selector>
Hope my answer will be helpfull!
Solution 3:
<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark">@color/colorPrimaryDark</item><itemname="colorAccent">@color/colorAccent</item><itemname="colorControlNormal">@color/spinner_background</item></style>
Define Spinner_background color in color folder..
Solution 4:
Try creating a selector in the drawable, something like,
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="false"android:drawable="@color/colorPrimary" /><itemandroid:drawable="@android:color/transparent" /></selector>
And set the spinner background as
android:background="@drawable/spinner_selector"
Solution 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"?><TextViewxmlns: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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- Activated state is the selected item --><itemandroid:state_activated="true"android:drawable="#00ff00"/><!-- Pressed state is the one the user is interacting with --><itemandroid:state_pressed="true"android:drawable="#00ff00"/><!-- The rest of the items --><itemandroid:drawable="#ffffff"/></selector>
Post a Comment for "Change Background Color Of The Selected Item In Android Spinner"