user3781214 user3781214 - 1 month ago 9
Android Question

Android add arrow image to spinner

I have created a background drawable that I use for a spinner. I would like to now but an arrow image inside this background. I have tried things like android:drawableRight="arrowimage", but this does not seem to show anything. Does anyone know how I can include an image in my custom background for a spinner?

Answer

Create an XML in drawable folder with any name for example spinner_bg.xml and add the following lines

<?xml version="1.0"
      encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <layer-list>
      <item>
        <shape>
          <gradient android:angle="90"
                    android:endColor="#ffffff"
                    android:startColor="#ffffff"
                    android:type="linear" />

          <stroke android:width="1dp"
                  android:color="#504a4b" />

          <corners android:radius="5dp" />

          <padding android:bottom="3dp"
                   android:left="3dp"
                   android:right="3dp"
                   android:top="3dp" />
        </shape>
      </item>
      <item>
        <bitmap android:gravity="bottom|right"
                android:src="@drawable/spinner_ab_default_holo_dark_am" />
        // you can use any other image here, instead of default_holo_dark_am
      </item>
    </layer-list>
  </item>
</selector>

Add the following lines to your styles.xml which is inside values folder

<style name="spinner_style" >
        <item name="android:background">@drawable/spinner_bg</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginBottom">10dp</item>
        <item name="android:paddingLeft">8dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">5dp</item>

Now add this style to your spinner as

<Spinner android:id="@+id/spinner1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         style="@style/spinner_style"
         android:popupBackground="#cccccc" />