Android Spinner : Avoid onItemSelected calls during initialization

Just put this line before setting the OnItemSelectedListener

spinner.setSelection(0,false)

This works because setSelection(int, boolean) calls setSelectionInt() internally so that when the listener is added, the item is already selected.

Beware that setSelection(int) won't work, because it calls setNextSelectedPositionInt() internally.


spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.

So first time handle this with any Integer value

Example: Initially Take int check = 0;

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
   if(++check > 1) {
      TextView textView = (TextView) findViewById(R.id.textView1);
      String str = (String) parent.getItemAtPosition(pos);
      textView.setText(str);
   }
}

You can do it with boolean value and also by checking current and previous positions. See here