Redeem Now
Read More
Read now

Displaying Error if no item from a spinner is selected


Hello Everyone,
This blog is quite easy to understand and implement.
We all use spinner to display a list of selectable options and make our app interface more interactive .
In order to make app UI more interactive, we all try to set and point the error exactly at the view which is left empty. Let’s see how to do this on a spinner.


Concept : 
A Spinner in android is actually a collection of textviews that are listed according to the number of entries in the list.
All you need to do is, to check if the spinner is empty and then select the default text view and set error message on that text view.

How to do this :

  1. Check if spinner is empty.
  2. Get the default selected view.
  3. Request Focus for the same view.
  4. Set some error message to be displayed.
  5. You can also perform a click on the spinner so that the spinner list is displayed. (additional option).


calling error method if spinner is empty :

spinner error method :

And it is done .
Check it out and I believe this makes UI better.

Keep coding and Keep Sahring.


. . .

Comments (6)

Add Your Comment

  • Muhammad Faisal
    Hello man, i tried to implement that script but getItemAtPosition after String actualPositionOfMySpinner = (String) mySpinner. won’t showing.

    Why is it?
    Any mistyped?


    • anchit
      Hello there,

      Can you share the exact code that you are using ??

      Also, for debugging yourself you can try to have a look at these points.

      1) You are getting spinner object correctly. (By logging the instance of spinner, it should not be null ).
      2) printing the value of actualPositionOfMySpinner variable.
      3) Try calling the spinner error directly.

      If still there is something wrong, then Please do share the code snippets you are using and i will try to help you as soon as possible.

  • Joshua Swiss
    This is awesome! Thanks, I’d really been struggling with getting an error to show properly on my spinners until I found this
    • anchit
      You are Welcome
  • Hayat
    Thanks for this great solution!
    • anchit
      Your Welcome
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again