JuiCe JuiCe - 2 months ago 11
Android Question

Set Spinner to Unselected

I currently have a whole bunch of spinners in my application. I wasn't thinking much and for each spinner, the first index is filled with

N/A
. It's not a big deal, but I was hoping to make it a bit cleaner.

I was wondering if there was a way that instead of calling
setSelection( index )
, I wanted to know if there was a way to set a Spinner to have nothing selected.

Basically, I want the spinner to have a list of items within it, but display none of them until you click on it. I also don't want there to be a blank index at the top.

I think its quite possible that there is no way to do this, so if anyone is sure of this, please let me know.

Thanks.

Answer

spinners have to have a selected state. i would recommend having a "no selection" option

Another option is to have the bottom button selected and then set that one to be invisible (the reason i set the last one to selected is so that you dont have a chunk of blank space in your app as that looks a bit trashy):

case SPINNER_OPTION_FIRST:
    yourSpinnerLabel.setVisibility(View.VISIBLE);
    yourTextField.setVisibility(View.VISIBLE);
    break;        

// do this for all your other ones if you want

case SPINNER_OPTION_LAST:
    yourSpinnerLabel.setVisibility(View.GONE);
    yourTextField.setVisibility(View.GONE);

maybe not the most efficient but it works so i hope that helps (still new so cut me some slack)!

Comments