Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 241
Ollie Strevel Avatar asked Dec 16 '15 21:12

Ollie Strevel


1 Answers

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>
like image 83
TheoKanning Avatar answered Sep 19 '22 16:09

TheoKanning