Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner - How to position dropdown arrow as close to text as possible when options have different length?

The options in my spinner has different length and currently the dropdown arrow is positioned far to the right based on the longest option, as shown in the screenshot below.

enter image description here enter image description here

Is it possible to move the dropdown arrow so that it is dynamically positioned based on currently selected option?

Especially when the first option is just 'All', it looks weird when the dropdown arrow is so far away to the right.

Referring to Google Translate App where dropdown arrow is always positioned next to its text: enter image description here enter image description here

like image 823
Bruce Avatar asked May 08 '16 08:05

Bruce


People also ask

How do you move the android spinner drop down arrow closer to its emitting text?

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.

Which controls works as a drop down list in Android?

In Android, Spinner is used to select one value from a set of values. In the default state, a spinner shows its currently selected value. Touching the spinner displays a drop down menu with all other available values, from which the user can select a new one. Android spinner is associated with AdapterView .

How do I hide spinner text?

You can hide spinner in the ProgressButton by setting the e-hide-spinner property to cssClass . Is this page helpful?


1 Answers

You cannot control the position of the original dropdown icon, the only way is to disable the default icon and add your own one into the dropdown.

First, disable the default dropdown icon by setting the background of the Spinner to @null:

<Spinner
     android:id="@+id/spinner_main"
     android:spinnerMode="dropdown"
     android:background="@null"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"/>

Then create a layout resource spinner_item_main.xml with only one TextView which we can set a drawable on its right side (you can download the arrow picture from here):

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:gravity="left"
    android:textColor="@color/colorWhite"
    android:drawableRight="@drawable/ic_arrow_drop_down_white_24dp"
    />

Finally Set this layout resource when you initialize the Spinner, You can also provide a resource as the dropdown view (as what I have done):

(I use kotlin)

spinner_main.adapter = ArrayAdapter<String>(this,
            R.layout.spinner_item_main, objects).apply {
        setDropDownViewResource(R.layout.spinner_dropdown_view_main)
    }

Make it! View this in My APP

like image 149
xiaoyu Avatar answered Oct 10 '22 09:10

xiaoyu