+ Reply to Thread
Results 1 to 2 of 2

Thread: How to make an Android Spinner with initial text Select One

  1. #1
    Junior Member
    Join Date
    Jul 2012

    How to make an Android Spinner with initial text Select One

    In Android, I want to use a Spinner that initially (when the user has not made a selection yet) displays the text "Select One". When the user clicks the spinner, the list of items is displayed and the user selects one of the options. After the user has made a selection, the selected item is displayed in the Spinner instead of "Select One".

    I have the following code to create a Spinner:
    String[] items = new String[] {"One", "Two", "Three"};
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);
    With this code, initially the item "One" is displayed. I could just add a new item "Select One" to the items, but then "Select One" would also be displayed in the dropdown list as first item, which is not what I want.

    Any ideas? Thanks in advance.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    First, you might be interested in the "prompt" attribute of the Spinner class. See the picture below, "Choose a Planet" is the prompt that can be set in the xml with android:prompt=""

    I was going to suggest subclassing Spinner, where you could maintain two adapters internally. One adapter that has the "Select One" option, and the other real adapter (with the actual options), then using the OnClickListener to switch the adapters before the choices dialog is shown.. however after trying implement that idea I've come to the conclusion you cannot receive OnClick events for the widget itself.

    You could wrap the spinner in a different view, intercept the clicks on the view, and then tell your CustomSpinner to switch the adapter, but seems like an awful hack.

+ Reply to Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts