Reputation: 865
I want to detect when the user select the Spinner View, not the item inside spinner in Android. That is, I want to detect the event before the spinner opening. My intention is to open another Dialog when user select the Spinner, instead of open ths Spinner.
Thanks in advance,
Solved: https://stackoverflow.com/a/30406057/2562459
Upvotes: 0
Views: 686
Reputation: 865
Solved:
I solved this using the method setOnTouchListener() instead of setOnClickListener(). If we try to use setOnClickListener() it returns an error. But if we use setOnTouchListener() it runs without problems. Just keep in mind to use MotionEvent.ACTION_UP argument to avoid many selection events:
mSpinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// TODO - User pressed spinner
}
return false;
}
});
Upvotes: 2
Reputation: 17
Spinner does not support item click events.
Try this:
spinner.setOnTouchListener(Spinner_OnTouch);
spinner.setOnKeyListener(Spinner_OnKey);
listners:
private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
doWhatYouWantHere();
}
return true;
}
};
private static View.OnKeyListener Spinner_OnKey = new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
doWhatYouWantHere();
return true;
} else {
return false;
}
}
};
Upvotes: 1