Change spinner background color but keep arrow
For the record, I found an easy solution : Wrap your spinner in a relative layout and add an image :
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/borderbottom_white"<!-- white background with bottom border -->
android:layout_marginTop="15dp" >
<Spinner
android:id="@+id/postfield_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:background="@null"
android:minHeight="0dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/arrowspinner" />
</RelativeLayout>
A simple solution that doesn't require you to create your own drawable for the arrow is to wrap the spinner with a RelativeLayout
, and set the background color in the RelativeLayout
, not the spinner:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Use this:
yourspinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
((TextView) yourspinner.getSelectedView()).setBackgroundColor(getResources()
.getColor(R.color.your_color));
}
and your class should implement OnItemSelectedListener.
I did a little modification based on @Mansur Khan 's answer.
We don't have to add an ImageView in this case because the spinner already has a triangle arrow. So check the code below:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#FFFFFF">
<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/sign_up_country"
/>
</RelativeLayout>
Here is the screenshot
Before:
After: