Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change spinner background color but keep arrow

I've read several thing about it but I can't find what I need. I want to keep the grey arrow but I want to remove the horizontal bar from the default style and have a white background. Do you have any idea of how I can do this ?

Here is what I have now (default spinner style) :

enter image description here

Here is what I want :

enter image description here

like image 855
Mansur Khan Avatar asked Sep 16 '14 11:09

Mansur Khan


5 Answers

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: enter image description here

After: enter image description here

like image 170
Chandler Avatar answered Nov 19 '22 02:11

Chandler


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>
like image 25
Mansur Khan Avatar answered Nov 19 '22 04:11

Mansur Khan


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>
like image 36
Tad Avatar answered Nov 19 '22 03:11

Tad


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.

like image 4
Kishan Dhamat Avatar answered Nov 19 '22 04:11

Kishan Dhamat


Hi instead of wrapping Spinner component around Parent Layouts like LinearLayout, RelativeLayout etc which increases layout parsing simply create a drawable called spinner_bg.xml under drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/icn_dropdown_arw" />
    </item>
</layer-list>

Set spinner_bg as the background of your spinner and it works like charm:

<Spinner  
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="5dp"
     android:background="@drawable/spinner_bg" />
like image 3
Namrata Bagerwal Avatar answered Nov 19 '22 02:11

Namrata Bagerwal