Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage the height of android spinner items?

I have an android spinner that's populated by a list of strings using an ArrayAdapter and it operates fine, however because of the way the spinner is displayed I'm running into a display height problem with the list items.

At first glance, it would seem that the ArrayAdapter can use a single layout for displaying options which leads to the problem I'm having. When displaying the current item in the spinner (when the user is not selecting a new item from the list) the spinner pads the text so that the spinner is a reasonable size for clicking on. However, when the user taps on it and brings up the list to select a new item, the list items presented are way too small height-wise. If I use an item layout that presents the list items at a reasonable height, then the spinner itself becomes exorbitantly huge due to its own padding of the list item.

Any ideas on how I can manage the height of these two item display modes so that effectively they display with the same height value instead of the spinner height being larger than the list item display height?

like image 609
rushinge Avatar asked Jun 13 '10 18:06

rushinge


People also ask

What is the use of spinner control in Android application?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

How can I use more than one spinner in Android?

Clearly each of your adapters need to adapt a different set of String s, so you need to create an ArrayAdapter for each Spinner . A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?>

What is custom spinner in Android?

In Android, Spinners provides you a quick way to select one value from a set of values. Android spinners are nothing but the drop-downlist seen in other programming languages. In a default state, a spinner shows its currently selected value. It provides a easy way to select a value from a list of values.

How can I tell if spinner is clicked or not in android?

Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.


2 Answers

I've run into this issue myself a while ago, and it turned out that I need to use different layouts for dropdown and display

I have this code:

adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cGroups,
                new String[] {
                        "name", "_id"
                }, new int[] {
                    android.R.id.text1
                });
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
like image 141
Pentium10 Avatar answered Sep 28 '22 05:09

Pentium10


Yes, the above answer is correct.

It took me forever to find this, because it's wrong in the sdk samples for 2.2 Android. And I had a hard time accepting that.

Here's a snippet from samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java:

       this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
                android.R.layout.simple_spinner_dropdown_item);

while it should have android.R.layout.simple_spinner_item there instead and simple_spinner_dropdown_item should only be used for the dropdown items. Otherwise the spinner arrow get streched and it draws dropdown selection circle to the display, too.

like image 27
Tommi Kyntola Avatar answered Sep 28 '22 07:09

Tommi Kyntola