Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override onClick on Android Spinner?

Tags:

android

A normal android Spinner will pop up a list of choices when clicked. I want to override this onClick. When the user clicks the spinner during certain error states, I want to display an error message rather than pop up the list of choices. Currently, all I can do is set a OnClickListener, but its onClick method doesn't let me prevent the list of options from appearing.

like image 991
JoJo Avatar asked Dec 08 '11 23:12

JoJo


2 Answers

Try setting a onTouchListener and in the onTouch method display your popup and return true to consume the event and stop it from propagating to the view (Spinner in this case).

spinner.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
   // display your error popup here
   return true;
   }
});

This should stop the "drop down list" from appearing.

Edit: forgot to mention you could do your error state check in the onTouch method as well, so you don't completely disable the spinner.

like image 182
Tony Chan Avatar answered Oct 03 '22 06:10

Tony Chan


Extends from Spinner and override performClick() like this:

@Override
public boolean performClick() {
   if(errorOccured) {
       // show validation message
       return true; // the event is handled by ourselves
   }
   else {
       return super.performClick(); // show spinner dialog
   }
}

See sources for more details. Hope this helps.

like image 40
Flavio Avatar answered Oct 03 '22 08:10

Flavio