Search code examples
androidandroid-edittextspinnerhint

Android Spinner hint?


I have two spinners in my current android app, I would like to have a default value like the editText's android:hint ability. Is there a way to do so, but in a way that doesn't add the hint to the string array that populates the spinner. e.g. it's impossible to choose the hint from the spinner list.

Also, is there a way to check if the user has selected a valid value and if not take an action. I do this in my editTexts by getting the value to a string and then:

if (calculator_ShutterSpeed_TempValue.equals("") ){ action }

And can I set the spinner back to it's hint value on another button press?

So what I am for asking is:

  1. Spinner hint that is not possible to be redisplayed after the user has clicked on the spinner?
  2. Check if the user has selected a valid value from the spinner, (have they selected a value beyond the default hint one)?
  3. Redisplay the hint on click of a reset button?

Solution

  • To set the default value in Spinner you have to set first that index selected setSelection(index)and save it to the preferences by using SharedPreferences. Please check out the code to set and load the preferences. So onResume() highlight that index value selected.

    To get the value from spinner use spinner.getSelectedItem() and cast it to String. Now you can compare it to your criteria.

    To redisplay the hint, first it must be as an item in spinner and then regiter the OnItemSelectedListener. In that Listener's code reset the selected index to yout hint index again.