java – Add more space between items in Android Spinner without custom style?-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

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.

###

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"

###

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

###

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, 
ArrayList);

Leave a Reply

Your email address will not be published. Required fields are marked *