-1

How to create spinner with drop down list like here:

|------------|
| option1    |
|------------|
| option2    |
|------------|
| option3    |
|------------|
| create new |
|------------|

On clicking from 'option1' to 'option3' - just clicked item is set as a spinner value.
But when we click 'create new' then:

  • drop down list is closed
  • nothing is selected [spinner retain primal value]
  • some 'external action is called' [which I inject outside of spinner]

UPDATE
Solution like below Juanjo's https://stackoverflow.com/a/28612543/1367449 generally do the job but has a drawback: between user's click on | create new | and setting spinner.setSelection(previousSelection) the spinner shows 'create new' value, and only then switches to "previous selection". Although this swich lasts for a fraction of a second then it cause a 'spinner blink effect' which hope we can eliminate.

Community
  • 1
  • 1
Grzegorz Dev
  • 3,073
  • 1
  • 18
  • 22
  • possible duplicate of [How to make an Android Spinner with initial text "Select One"](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – Juanjo Vega Feb 19 '15 at 22:20

1 Answers1

0

Add an extra empty option to your spinner and register an ItemSelectedListener:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        if(position == 4) { // empty (=0), option 1, 2, 3, new (=4)
            spinner.setSelection(0); // empty item
            external_action();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // Stuff to do
    }
});
Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • Bad answer. I tested it earlier. Between user's click on item "create new" and executing "spinner.setSelection(0)" spinner shows value "create new". It last for a fraction of a second but is visually ugly. – Grzegorz Dev Feb 19 '15 at 19:28
  • Muy answer does exactly what you asked for. Good luck. – Juanjo Vega Feb 19 '15 at 22:19