1

I built a search widget based on the code, Searchable Dictionary v2. Everything is working fine, but there is one problem.

If I rotate the screen from portrait to landscape or vice versa when the search dialog box is on, the text edit box and keyboard screen get disappeared and runs again.

Is there any way that I can make that it switches the edit box into appropriated mode(landscape or portrait) without exiting and re-running the dialog box?

You can check the behavior with the searchable dictionary. I want something like the google search widget. It just switches edit box mode while it keeps the screen.

juniano
  • 354
  • 4
  • 13

2 Answers2

5

Once the screen orientation is changed, Android creates a new activity/view, so you'll may have to handle screen orientation by yourself. I used this solution in an app successfully, I assume it also applies to widgets.

See this page on how to do that:
http://www.androidpeople.com/android-how-to-handle-screen-orientation-change-issue/

Some other helpful hints (basically the same solutions) are here:
How do I handle screen orientation changes with an activity started within a tab's activity
How to handle screen orientation change when progress dialog and background thread active?

Some information concerning onPause() and onSaveInstanceState() (which may not apply as you're using a widget, but just in case :) ) is here:
How do I disable orientation change on Android?

Community
  • 1
  • 1
Select0r
  • 12,234
  • 11
  • 45
  • 68
  • url no longer works. Can you describe the solution? I try and recreate the search process, to the point of expanding the search action in the toolbar, and entering the users search text. This works until I complete the search. At that point the icons in the toolbar vanish. The navigation icon and overflow (three dots) icon remain, but my search icon (and upload icon) there were there are gone. They are restored on another screen rotation – Brian Reinhold Feb 23 '18 at 14:03
2

What i did was to lock the orientation while the dialog is displayed, then unlock it when finished:

int currentOrientation = getResources().getConfiguration().orientation;
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); //locks landscape
        }
        else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); //locks port
        }
    // do work
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Blazemonger
  • 90,923
  • 26
  • 142
  • 180