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

5

21

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.

Quarta answered 23/3, 2013 at 20:35 Comment(0)
Q
29

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.

Quarta answered 28/3, 2013 at 17:48 Comment(0)
F
22

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.

Frostbite answered 23/4, 2015 at 6:11 Comment(2)
This answer, in addition to being 2 years late, in no way addresses the actual question.Quarta
Actually it works, the only problem is that it also gives some extra spacing to the Spinner itself.Megasporophyll
W
12

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

Whereat answered 5/2, 2019 at 2:17 Comment(0)
M
7

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"
Macdonald answered 23/3, 2013 at 21:29 Comment(6)
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.Quarta
@Quarta But the style parent should be android:style/Widget.TextView.SpinnerItemMacdonald
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.Quarta
@Quarta 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 #15299694Macdonald
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!Quarta
I dont think this answer works, it just add padding to the spinner itself not the itemsBoard
T
2
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, 
ArrayList);
Teddie answered 23/6, 2018 at 16:42 Comment(1)
"R.layout.simple_dropdown_item_1line" will change the list view style @AlexUngerTeddie

© 2022 - 2024 — McMap. All rights reserved.