I have a spinner which is populated from the database. So if the spinner is clicked with no items in it,a spinner with an empty list item is shown.I want to avoid showing the spinner when it is empty and show a toast message. I have the logic to check the count of the items and show the toast message, but I cannot find out where to call the function.What is the event called when clicking the spinner and loading the spinner items?
-
I added some code, for me it works entirely. – stealthjong Dec 05 '12 at 14:54
3 Answers
Why not set an onClickListener
?
Spinner s = (Spinner)findViewById(R.id.myspinner);
s.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
checkForItemCount(); //where you show your Toast if it's empty.
}
});
Edit: I think I do not completely cover your question, so I tried some code myself. This works entirely for you.
final Spinner s = (Spinner)findViewById(R.id.spinner1); // get reference
ArrayList<String> mList = new ArrayList<String>(); //some list without items
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mList));
s.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && mList.size() == 0) {
toast("Empty"); //lazy method for toasting
s.setPressed(false); //closes the spinner.
}
return true;
}
});

- 10,858
- 13
- 45
- 84
-
I tried this,but the error message shows that you cannot set onclicklistener for the spinner – viks Dec 05 '12 at 14:34
-
Try using `s.setOnClickListener(new View.OnClickListener() {` instead – Mohamed_AbdAllah Dec 05 '12 at 14:37
-
This works but the toast message is shown 3-4 times...Is touchlistener called whenever setselection is called? – viks Dec 05 '12 at 15:03
-
@viks In that case, just use the setOnClickListerer(). The touchListener might not be a good idea. – stealthjong Dec 05 '12 at 15:04
-
checking if (event.getAction() == MotionEvent.ACTION_UP) in ontouchlistener works for me – viks Dec 05 '12 at 15:09
-
1@viks Great idea, I put it in the code. If you ever want to open your spinner (when the spinner if filled, for example, call `s.performClick()`). – stealthjong Dec 05 '12 at 15:14
Spinner
is a subclass of View
, so, View.setOnClickListener()
can be used to change onClick()
implementation which will be called when it is clicked. You can override onClick()
to check if the Spinner
is empty and then show the Toast
.
Correction:
Since you cannot setOnClickListener
for Spinner
, you can use the solution suggested in this question (the contents of which I've pasted below).
Instead of setting the spinner's OnClickListener, we are setting OnTouchListener and OnKeyListener.
spinner.setOnTouchListener(Spinner_OnTouch);
spinner.setOnKeyListener(Spinner_OnKey);
and the listeners:
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;
}
}
};

- 1
- 1

- 5,311
- 3
- 28
- 47
-
The error message shown is 'Don't call setOnClickListener on AdapterView.You probably may want to use setOnItemClickListener.'.But this is not suitable for my purpose. – viks Dec 05 '12 at 14:49
This is the function when an item is selected if that's what you were asking. For more help you will need to post your code. Otherwise, we have no idea what you have going on
spinner.setOnItemSelectedListener(new OnItemSelectedListener()

- 44,549
- 13
- 77
- 93
-
No,I want to avoid showing the spinner items if the count is 0. I have to check when the user clicks on the textbox like view shown in the place of spinner – viks Dec 05 '12 at 14:31