Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner: Remove Extra White Space Between Text and Dropdown Icon

I have a spinner and by default there is extra white-space between text and dropdown icon which I really don't like and wanna remove it.

Tried searching over the web but did not get anything which could help me. Anybody here who has done it earlier?

Spinner

like image 866
Akif Patel - BRMS Avatar asked Aug 23 '17 16:08

Akif Patel - BRMS


4 Answers

set gravity to end and adjust it with an appropriate value :

  android:gravity="end"
  android:paddingEnd="20dp"
like image 86
Mohamed AbdelraZek Avatar answered Sep 30 '22 12:09

Mohamed AbdelraZek


I did it myself after playing with Spinner. Here is the solution which worked pretty well.

First create a Dropdown with indicator image of your choice.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        android:shape="rectangle"/>
</item>
<item
    android:width="24dp"
    android:height="24dp"
    android:gravity="right|center_vertical">
    <bitmap
        android:src="@drawable/ic_dropdown"
        android:tint="@color/colorPrimaryDark"/>
</item></layer-list>

Then assign it to the background of Spinner.

<Spinner
        android:id="@+id/basket_spinner_item_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"
        android:padding="0dp"
        android:background="@drawable/ic_spinner_dropdown"/>

Now, adjust the padding and alignment of spinner item as per your requirement.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simple_spinner_text_quantity"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="25dp"
android:gravity="right"
android:textAlignment="gravity"/>
like image 38
Akif Patel - BRMS Avatar answered Sep 30 '22 12:09

Akif Patel - BRMS


Simply just add the following attribute to your spinner xml element:

For API < 17:

android:gravity="right"

For API => 17:

android:gravity="end"
like image 29
Wilhelm Avatar answered Sep 30 '22 10:09

Wilhelm


Use below code for Quick fixed

 <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-9dp"
        android:layout_marginTop="10dp"
        android:entries="@array/almanac"
        android:gravity="left"
        android:padding="0dp"
        android:spinnerMode="dropdown"
        android:layout_marginStart="-9dp" />
like image 29
Mujahid Khan Avatar answered Sep 30 '22 11:09

Mujahid Khan