I found a very ugly solution to this (the 'action' takes place in the onConfigurationChanged method):
Before calling setContentView, check if the dropdown view is shown (*) and if so, save the position that is currently selected in the spinner (int pos = spinner.getSelectedItemPosition()
).
After calling setContentView and setting the spinner adapter, if the dropdown view was shown in step 1, force the dropdown view to show by calling performClick on the spinner:
spinner.setSelection(pos);// this way we make sure that the same item
// remains selected after rotating the device
spinner.performClick(); //show the dropdown view
(*) Checking if the dropdown view is shown is the trickier part. I haven't found (yet) a method that lets me know whether the dropdown view is shown, so I had to do the following:
Hold the spinner's pressed state in a boolean variable (named, for example, isClicked).
Set an onTouchListener for the spinner and in the onTouch method set isClicked to true (when tapping the spinner, the dropdwon view opens, so isClicked == true means that the dropdown view is shown).
Override onKeyDown or onKeyUp and when back button is pressed, if isClicked is true, set it to false (I assumed that pressing back with isClicked==true means closing the dropdown view).
Use the value of isClicked in onConfigurationChanged method to check if the dropdown view is shown.
Like I said, it's an ugly fix, but it's the only one I could come up with until now. If anybody has other ideas, please post them.
Humbly taken from here
There are other solutions too which are as follows
- SO answer 1
- SO answer 2
Bottom Line is you need to capture the status
(some boolean variable) when the spinner is opened and when on activity configChange
need to intialize the spinner state from the status
Hope it helps!!