title: How to set selected item for Spinner in Android
body: <p>In an Android app, I have a <code>SpinnerAdapter</code> that I am using to display expiration years of credit cards. My variable <code>expirationYears</code> contains an array of Strings with years from <code>"2020"</code> to <code>"2029"</code>. It was declared as <code>String[] expirationYears = new String[10];</code> and then with a function I set the ten years as Strings, from current year and then the nine subsequent years after that for a total of ten elements in the array. When I run it, this is what I see:</p>
  
  <p><a href="https://i.stack.imgur.com/6IuzH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6IuzH.png" alt="enter image description here"></a></p>
  
  <p>Everything is perfect so far. The only problem is that when as you see in the image above, <code>2020</code> is the year selected by default. That is the first element in the <code>expirationYears</code> array. But I want to set <code>2021</code> as default (the second element of the array). This is what my code has:</p>
  
  <pre><code>Spinner spinnerYearCreditCardPayment;
  SpinnerAdapter creditCardYearAdapter = new SpinnerAdapter(Payment.this, expirationYears);
  creditCardYearAdapter
      .setDropDownViewResource(android.R.layout.spinner_dropdown_item);
  spinnerYearCreditCardPayment.setAdapter(creditCardYearAdapter);
  spinnerYearCreditCardPayment
      .setOnItemSelectedListener(new OnItemSelectedListener() {
          ..........
      });
  </code></pre>
  
  <p>I was expecting to have some property available to define which element I want to show by default. Something like <code>creditCardYearAdapter.setSelectedItem(INDEX_NUMBER)</code>. But that property does not exist. Do you know how I could set the default selected item so that it is not the first element of the array (2020) but the second (2021)? Thank you.</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Following The_Martian's answer, this was what I did:</p>
  
  <pre><code>spinnerYearCreditCardPayment
      .setOnItemSelectedListener(new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
             arg0.setSelection(1);
             cardYear = (String) spinnerYearCreditCardPayment
                 .getSelectedItem();
          }
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
              // TODO Auto-generated method stub
          }
      });
  </code></pre>
  
  <p>Now I correctly see the second element of the array ("2021") as the default selection.</p>
  
  <p>The problem is that when I use <code>arg0.setSelection(1);</code>, I choose another year and it does not respond. The selected year remains "2021" even thought I am trying to change the year.</p>