0

I have a Spinner in my action bar which gets initialized only once in onCreateActionMode();

Issue: When the spinner drop down is visible and I try to rotate the screen, the drop down state is not maintained after orientation change.

How could I maintain the state so that if in Portrait mode the Spinner dropdown is visible then after orientation change in Landscape mode too the dropdown should be visible.

Also let me know the API's by which we check whether the drop down is visible or not at a given point. Also an API to show the drop down forcefully.

I have also specified configChanges="orientation" in my Manifest file.

Vaibs
  • 1,128
  • 3
  • 16
  • 36

1 Answers1

0

I found a very ugly solution to this (the 'action' takes place in the onConfigurationChanged method):

  1. 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()).

  2. 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

  1. SO answer 1
  2. 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!!

Community
  • 1
  • 1
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93