user11230 user11230 - 6 months ago 45
Android Question

How to let user choose a specific year from a spinner

Hello I want the user to be able to click a spinner and choose a year from those I provide. I would provide a range from 1970 to current year, but I don't know how to implement it.

If I create a spinner and give it an array like this

<string-array name="spinnerItems">

Then I would need to add more than 40 items by hand, and I would need to update the app manually every year. Also the spinner onClick callback returns not the string value but the index of the string in the array, so for example 1972 would be index 3.

Also I will provide another spinner where the user will choose a year that's greater or equal of the year choosen in the first spinner.

In the end I want tos end an api request with an interval of years.

How can I do this?


I think you have to create year picker instead of creating number list of spinner

Try this

 ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);

Spinner spinYear = (Spinner)findViewById(;

Hope this will help..