13

The first year from the data array is shown instead of the text from prompt in my spinner. I tried adding the prompt in XML, but I also tried from code. Furthermore, it gives me a "resource not found error", when adding the spinnerSelector attribute.

XML

<Spinner
    android:id="@+id/spinnerYear"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:drawSelectorOnTop="true"
    android:padding="5dip"
    android:prompt="@string/spinner_header"
    android:background="@drawable/selector_yearspinnerback"
    android:layout_below="@+id/linearLayout_gender_btns"
    android:layout_centerHorizontal="true"></Spinner>
  -- android:spinnerSelector="@drawable/category_arrow"

Code

ArrayList<String> yearList = new ArrayList<String>();
int now = new Date().getYear() + 1900;
for (int i = now; i > now - 110; i--) {
    yearList.add(i + "");
}
Spinner spinner = (Spinner) findViewById(R.id.spinnerYear);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Crunch
  • 2,823
  • 5
  • 18
  • 17

3 Answers3

42

Perhaps you are seeing the spinner drop down items as list without any prompt text. There are two modes in which spinner shows the items, dropdown and dialog.

Add this attribute to your spinner as an XML atrtribute:

android:spinnerMode="dialog"

And you will now get items in a popup dialog select list instead of drop down list.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
1

You have to set adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); after

spinner.setAdapter(adapter);

So the fixed code would be:

ArrayList<String> yearList = new ArrayList<String>();
int now = new Date().getYear() + 1900;
for (int i = now; i > now - 110; i--) {
    yearList.add(i + "");
}
Spinner spinner = (Spinner) findViewById(R.id.spinnerYear);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearList);
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

(I hope it works for you like it works for me :D!)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gkenny
  • 51
  • 7
0

For me, both android:prompt XML attibute as well as Spinner.setPrompt work, and list selector displays correct title.

Try to find bug in your code, or make call to Spinner.getPrompt at some point and print this to log, to find our from where you get invalid title.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111
  • Logged the value of getPrompt after the spinner.setAdapter(adapter); and it shows the correct string in LogCat. – Crunch Oct 03 '11 at 08:43
  • So try to use debugger - step into Spinner.java (assumes that you download Android source code), and see what's in mPrompt variable when it's used in builder.setTitle(mPrompt); – Pointer Null Oct 03 '11 at 08:59