How can you set the event listener for a Spinner when the selected item changes?
Basically what I am trying to do is something similar to this:
spinn
Find your spinner name and find id then implement this method.
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Some of the previous answers are not correct. They work for other widgets and views, but the documentation for the Spinner widget clearly states:
A spinner does not support item click events. Calling this method will raise an exception.
Better use OnItemSelectedListener() instead:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
This works for me.
Note that onItemSelected method is also invoked when the view is being build, so you can consider putting it inside onCreate()
method call.
One trick I found was putting your setOnItemSelectedListeners in onWindowFocusChanged instead of onCreate. I haven't found any bad side-effects to doing it this way, yet. Basically, set up the listeners after the window gets drawn. I'm not sure how often onWindowFocusChanged runs, but it's easy enough to create yourself a lock variable if you are finding it running too often.
I think Android might be using a message-based processing system, and if you put it all in onCreate, you may run into situations where the spinner gets populated after it gets drawn. So, your listener will fire off after you set the item location. This is an educated guess, of course, but feel free to correct me on this.
Spinner spnLocale = (Spinner)findViewById(R.id.spnLocale);
spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Your code here
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
Note: Remember one thing.
Spinner OnItemSelectedListener
event will execute twice:
Try to differentiate those two by using flag variable.
It doesn't matter will you set OnItemSelectedListener in onCreate or onStart - it will still be called during of Activity creation or start (respectively).
So we can set it in onCreate (and NOT in onStart!).
Just add a flag to figure out first initialisation:
private Spinner mSpinner;
private boolean mSpinnerInitialized;
then in onCreate (or onCreateView) just:
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (!mSpinnerInitialized) {
mSpinnerInitialized = true;
return;
}
// do stuff
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
The docs for the spinner-widget says
A spinner does not support item click events.
You should use setOnItemSelectedListener
to handle your problem.