How to move the Android Spinner drop down arrow closer to it's emitting text

Ollie Strevel picture Ollie Strevel · Dec 16, 2015 · Viewed 8.8k times · Source

I need to set the drop down arrow closer to its emitting text element in Spinner. How can achieve this?

<Spinner android:id="@+id/spinner"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

enter image description here

Answer

TheoKanning picture TheoKanning · Dec 16, 2015

You can fix this by defining a custom background in xml, and then setting the arrow's margin from the right side.

Start by defining a layer-list with a rectangle background and a bitmap object for your arrow. You can make the arrow align to the center on the right side by setting its gravity, and you can move it towards the center by setting its right margin via the android:"right" attribute. Note that this won't dynamically move the arrow based on the length of text, but it should be a helpful first step.

spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/color_white" />
            <corners android:radius="2.5dp" />
        </shape>
    </item>
    <item android:right="64dp">
         <bitmap 
             android:gravity="right|center_vertical"  
             android:src="@drawable/ic_spinner" />
    </item>
</layer-list>