48

I'd like to be able to call code like this, similar to how setError is set on a TextView:

spinner.setError("Error message");

However, setError only works for an EditText, not for a Spinner.

I want to notify the user if the spinner field is not selected. How can I perform such a notification without using a Toast?

CJBS
  • 15,147
  • 6
  • 86
  • 135
Gopinath S
  • 518
  • 1
  • 4
  • 12
  • possible duplicate of [Creating a setError() for the Spinner](http://stackoverflow.com/questions/3749971/creating-a-seterror-for-the-spinner) – Axalo Jan 30 '15 at 12:24
  • Seterror method is not available for spinner..u have to create other function or toast message to show it. – zyonneo Mar 06 '15 at 09:03

4 Answers4

88

There are a few solutions in this thread Creating a setError() for the Spinner:

The EdmundYeung99's one works for me, either you are using your own adapter or not. Just put the following code in your validate function:

TextView errorText = (TextView)mySpinner.getSelectedView();
errorText.setError("");
errorText.setTextColor(Color.RED);//just to highlight that this is an error
errorText.setText("my actual error text");//changes the selected item text to this

But, make sure you have at least one value in the Spinner adapter when you are doing your verification. If not, like an empty adapter waiting to be populate, make your adapter get an empty String:

ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, new String[]{""});
mySpinner.setAdapter(adapter);
Community
  • 1
  • 1
Lionel T.
  • 1,194
  • 1
  • 13
  • 17
40

Spinner class will return a textview when you use getSelectedView(). So you can use setError() indirectly.

((TextView)spinner.getSelectedView()).setError("Error message");

Results should be like ...

setError in spinner

Hope It will be helpful!

User
  • 4,023
  • 4
  • 37
  • 63
  • 2
    Very good idea but it breaks if no item is selected, in that case it returns null. – Javier Mendonça Aug 12 '16 at 10:22
  • 1
    Solution is not about to satisfy all the cases. There may be several other conditions and use cases. It's depend on you how you're making use of this snippet. :) – User Sep 05 '17 at 07:42
  • This only shows that there is an error on the spinner. It doesn't allude to what the error is... – marienke Sep 29 '17 at 07:26
  • I've just point out how setError() can be used with spinner. Even you also know set error is not a good approach to show error of spinner. I've just answered the question. If you really want to show message immediately then why are you not requesting focus on it! – User Sep 29 '17 at 07:48
3

Here is a solution that uses a hidden TextView to get a pop-up message to appear, in addition to the error icon in the spinner. When in an error state, the Spinner looks like this:

Spinner in invalid state

When not in an error state, it looks like this.

Spinner in valid state

The complete solution is documented here: https://stackoverflow.com/a/29956372/3063884

Community
  • 1
  • 1
CJBS
  • 15,147
  • 6
  • 86
  • 135
3

for people who are looking for a Kotlin answer

val errorText = spinnerclient.selectedView as TextView
                errorText.error = "client required"
                errorText.requestFocus()
                return@setOnClickListener

The focus was returned but the text wasn't displayed. I will update once its displayed

vishal N
  • 163
  • 10