Add more space between items in Android Spinner without custom style?

38,002

Solution 1

I believe Pragnani's answer is correct, but this is how I actually implemented it...

-In RES/layout, created an XML layout with just a textview in it, like shown below. This textview has the custom size / padding that I want.

spinner_row.xml

  <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/cust_view"
        android:minWidth="246dp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="left|center_vertical"
        android:textColor="@android:color/black"    
        android:textSize="20sp"
        android:paddingLeft="4dp"
        android:textIsSelectable="false"/>

Then in the activity where I load my data into the spinner, when I create an ArrayAdapter for my spinner, I pass the custom textview as the second parameter to the ArrayAdapter constructor.

Spinner spinClockInWorkSite = (Spinner)findViewById(R.id.spinClockInWorkSite);
ArrayAdapter spinClockInWorkSiteAdapter = new ArrayAdapter(this, R.layout.spinner_row, this.workSiteList);
spinClockInWorkSite.setAdapter(spinClockInWorkSiteAdapter);

So now the spinner uses my custom textview that's defined in spinner_row.xml for each item in the list.

This ended up being more straight-forward for my needs than playing with the style.

Solution 2

If you want to give satisfactory spacing then instead if all these XML and all, You can easily use "simple_spinner_dropdown_item" in spinner adapter.

Like

String [] dataList = new String[]{"Select","A","B","C","D","E"};
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, dataList);
        spinner.setAdapter(dataAdapter);

It worked for me.

Solution 3

Unless you want a really specific spacing, I would go with one of the built in layouts.

simple_spinner_item = No line space.
simple_spinner_dropdown_item = 1 line space.
simple_dropdown_item_1line = 1.5 line space.

These and more are found in R.class

Solution 4

Try this, Here you are not changing the spinner style, but adding the extra feature that you want for your spinner items by setting the style for Spinner Item.

<style name="spinnerStyle"  parent="@android:style/Widget.TextView.SpinnerItem">
       <item name="android:padding">5sp</item>
    </style>

And apply style to your spinner by using style attribue

 style="@style/spinnerStyle"

Solution 5

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, 
ArrayList);
Share:
38,002
Jim
Author by

Jim

Updated on February 05, 2020

Comments

  • Jim
    Jim about 4 years

    I have an Android application I'm writing for a Concrete company that will be used for clocking in and out. I've used some spinners to select things like Work Site and Job from drop downs, but I'm worried that the items in the spinners are too close together and will be difficult for the employees to select the right item.

    I'd like to give just a bit more space between the items in the spinner, but don't want to go through all the trouble of making a custom style, because really I want the spinner to look and behave exactly like the default except just having a little bit more padding, and it's a lot of work to have to make a custom style just for that.

    Is there any simple change I can make, like setting some property of the spinner? I've tried setting the spinner type to Dialog, but it just shows the list with the same amount of spacing, only not attached to the spinner control.

  • Jim
    Jim about 11 years
    I tried exactly this, but it didn't seem to do anything. I put the style in the styles.xml file in my res/values folder. I tried setting the padding value to 50 just so I could be sure there was a change, and nothing changed.
  • Pragnani
    Pragnani about 11 years
    @Jim But the style parent should be android:style/Widget.TextView.SpinnerItem
  • Jim
    Jim about 11 years
    I believe this is the correct answer, but has a minor mistake where it shows style="@style/spinnerStyleView" but the style in the code sample above is actually named spinnerStyle. I went a different route by using an XML in res\layout with just a textview in it and defined the textview's size and padding in the XML. Then when creating the adapter for my spinner, I passed by custom textview as the second parameter in the ArrayAdapter constructor, and it uses my custom textview for each item in the list. I thought this would be a difficult thing to implement but really it was quite simple.
  • Pragnani
    Pragnani about 11 years
    @Jim It was a typing mistake sorry for that...I was thought of suggesting custom layout for spinner but as you have asked for the simple solution then I decide to post this... check my previous answer here stackoverflow.com/questions/15299194/…
  • Jim
    Jim about 11 years
    Yeah, actually I used your previous answer to figure it out, but didn't notice until now that it was you who posted it. Thanks for both! I'll mark your answer correct for this one, and +1 on both your answers. Thanks!
  • Ahmed Galal
    Ahmed Galal almost 11 years
    I dont think this answer works, it just add padding to the spinner itself not the items
  • Jim
    Jim about 9 years
    This answer, in addition to being 2 years late, in no way addresses the actual question.
  • Anthony
    Anthony almost 9 years
    Actually it works, the only problem is that it also gives some extra spacing to the Spinner itself.
  • Govind Maheshwari
    Govind Maheshwari almost 6 years
    "R.layout.simple_dropdown_item_1line" will change the list view style @AlexUnger