Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Spinner: Remove padding to the right of the arrow

I'm using a spinner in a relative layout with a couple of other layout elements (layout-v21):

My spinner

<Spinner
android:id="@+id/spinner"
android:layout_width="155dp"
android:layout_height="34dp"
android:layout_alignBottom="@+id/textList"
android:layout_alignTop="@+id/textList"
android:layout_alignParentEnd="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_toEndOf="@+id/textList"
android:paddingRight="1dp"/>

This sets the distance to where the parent layout ends:

android:layout_marginRight="0dp"
android:layout_alignParentEnd="true"

This sets how close the choice items inside the spinner can get to the left of the arrow (right padding of the text):

android:paddingRight="1dp"

But is there a way to get rid of (or at least make smaller) the unnecessary space/padding that is to the right of the arrow but still within the spinner layout element (so I can then use more space on the left without changing the size of the spinner)?

This is what I'm talking about:

enter image description here

Edit: Here's the code for the RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mycompany.myapp.MainActivity"
android:backgroundTint="#000000"
android:focusableInTouchMode="true">
like image 487
Neph Avatar asked May 08 '18 14:05

Neph


1 Answers

It work for me :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:backgroundTint="#000000"
android:focusableInTouchMode="true">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="155dp"
        android:layout_height="34dp"
        style="@style/spinner_style"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="0dp"
        android:paddingRight="1dp"/>

styles.xml

 <style name="spinner_style">
        <item name="android:layout_height">35dp</item>
        <item name="android:background">@drawable/spinner_bg</item>
</style>

spinner_bg.xml

<?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="@android:color/white" android:startColor="@android:color/white" android:type="linear" />

                    <stroke android:width="1dp" android:color="@color/colorAccent" />

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

                    <padding android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp" />
                </shape>
            </item>
            <item>
                <bitmap android:gravity="center|right" android:src="@drawable/ic_spin_down_arrow" />
            </item>
        </layer-list>
    </item>

</selector>
like image 173
Rohan Lodhi Avatar answered Oct 23 '22 06:10

Rohan Lodhi