Can I make the spinner drop down list visible when changing orientation?

I don't know if it’s clear from the title that I asked, so here are a few steps to reproduce (assuming you have a layout with a counter):

  • A drop-down list will open.
  • Turn the device from portrait to landscape (or vice versa)> the list closes.

My problem is that I would like to keep the list open after turning the device. I know that this is usually possible if you override onConfigurationChanged , but I defined a different layout for landscape mode, so in my onConfigurationChanged method I need to call setContentView and set an adapter for the landscape counter that closes the drop-down list that was opened in portrait mode.

Is there a way to prevent the drop-down list from closing, or possibly forcing it to display after turning the device?

+4
source share
2 answers

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

  • Before calling setContentView , check if the drop-down list (*) is displayed, and if so, save the position that is currently selected in the counter ( int pos = spinner.getSelectedItemPosition() ).

  • After calling setContentView and installing the spinner adapter, if the drop-down list was shown in step 1, force the drop-down list to be displayed by calling Click 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 the display of the drop-down list is the more difficult part. I have not yet found a method that allows me to find out if a drop-down list is displayed, so I had to do the following:

  • Hold down the spinner button in a boolean variable (named, for example, isClicked ).

  • Set onTouchListener for the counter and in the onTouch method set isClicked to true (when clicking on the spinner, dropdwon opens, so isClicked == true means that a drop-down list is displayed).

  • Override onKeyDown or onKeyUp , and when you click the Back button, if isClicked is true, set it to false (I assumed that pressing isClicked == true means closing the drop-down list).

  • Use the isClicked value in onConfigurationChanged to check if a drop-down is displayed.

As I said, this is an ugly fix, but this is the only thing I could come up with so far. If anyone has any other ideas, send them to us.

+6
source

By default, the behavior when switching from portrait to landscape is restarted. Thus, you can save the spinner state in some place, for example, in SharedPreferences and read it when the activity is restarted (in the onCreate () or onResume () methods).

0
source

All Articles