Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android spinner prompt text not showing [duplicate]

Tags:

android

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);
like image 295
Crunch Avatar asked Oct 03 '11 08:10

Crunch


1 Answers

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.

like image 124
HimalayanCoder Avatar answered Sep 18 '22 06:09

HimalayanCoder