How to keep android search dialog on when rotating screen? - android

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.

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?

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);

Related

Android: Auto Rotate not working after setRequestedOrientation

I have a video player in which I have two features:
If the auto-rotate is disabled and when the user clicks on fullscreen button I force the orientation to go in landscape view using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) and back to portrait view when fullscreen is exited.
I have overridden the onConfigurationChanged to detect the change in orientation when the auto-rotate feature is enabled by user and change the activity view according to the orientation.
Now the problem with this setup is that if user start the activity and goes in fullscreen and setRequestedOrientation get invoked once, later when auto-rotate is enabled, the activity doesn't receive the orientation changes and the onConfigurationChanged doesn't get called.
I read here (https://stackoverflow.com/a/6109206/5167868) that they wont work simultaneously.
I would like to know if there a way of implementing both as I have seen players like MX Player do this in their app.
After you fix the orientation of the screen by using setRequestedOrientation(), Auto Rotate feature won't work. So to make it work, you may do something like this:
After you set your orientation using: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
Call this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
This shall make the auto rotate to work again!

Set initial screen orientation

I am making an app where the first screen I want to view is Portrait. I then go to the next activity which is set in landscape.
The problem i'm facing is that if I return to the first screen it will be set as landscape, evidently inheriting the orientation from the previous screen.
Is there a possible way to set the initial screen orientation as portrait which is set but the user can then go ahead and change it to landscape when they want?
Thanks
EDIT: SOLVED
Thank you for your suggestions. I've overcome the issue. It's slightly difficult to explain but will try my up most.
So my first activity has a feature whereby if the phone is turned to landscape it will show an image, full screen. And again, if the phone was portrait, the portrait view, where I have a list view.
The second activity loads an editing page to let me change that image.
So now if i press back on the editor activity. The first activity would load into a list view in LANDSCAPE. which is NOT what I want.
therefore in order to get around this. I have firstly disabled the back button when the first activity is landscape so the user can not remove himself from the view without turning the phone to landscape.
I then moved the code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Show landscape picture
}
}
to the top of my class before the onCreate method.
This has solved my issue so that now, if I was to go back to my first activity whilst in landscape the only thing that would show is the landscape view.
I hope that was clear enough for all to understand :)
Again thanks for the help!
you can set the screen orientation programmatically in the onCreate()- method:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
just right before the call of super.onCreate();.
I hope, I could help you.
Best regards,
G_J
You can change the orientation of individual activities using this in the manifest file of your application within each of your activity's tags:
android:configChanges="orientation"
with either
android:screenOrientation="portrait"
OR
android:screenOrientation="landscape"
as per your needs
you can set the screen orientation programmatically in the onCreate() method:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
just right after the call of super.onCreate();.
OR
You can also specify the orientation in the Manifest file like this
<activity android:name="com.example.project.activity"
android:screenOrientation="portrait"/>
Thank you for your suggestions. I've overcome the issue. It's slightly difficult to explain but will try my up most.
So my first activity has a feature whereby if the phone is turned to landscape it will show an image, full screen. And again, if the phone was portrait, the portrait view, where I have a list view.
The second activity loads an editing page to let me change that image.
So now if i press back on the editor activity. The first activity would load into a list view in LANDSCAPE. which is NOT what I want.
therefore in order to get around this. I have firstly disabled the back button when the first activity is landscape so the user can not remove himself from the view without turning the phone to landscape.
I then moved the code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Show landscape picture
}
}
to the top of my class before the onCreate method.
This has solved my issue so that now, if I was to go back to my first activity whilst in landscape the only thing that would show is the landscape view.
I hope that was clear enough for all to understand
Again thanks for the help!

Letting user change default orientation

I would like to allow the user to change the option of allowing Android to be displayed in portrait or landscape. What I do is allow the user to set a preference and then force it on the program.
At this point, I'm using code like so inside onCreate:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("landscapeModeEnabled", false)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onCreate(savedInstanceState);
The problem now is that it creates the Activity twice if the screen is not in the 'forced' orientation. That is, a phone in default (portrait) orientation will load the screen twice if the user wants a landscape orientation.
I know I can set this using XML, but that would not allow the user to change the preference of the orientation.
I'm using a SherlockFragmentActivity from Action Bar Sherlock, if that matters.
Have 2 Activities like this:
mActivityLand
mActivityPort
with different orientation in the Manifest.
Check the preference in previous Activity and launch respective Activity. Probably not the best solution :(

Empty EditText when turning screen on and Screen lock set to None

I'm having a problem where an EditText in landscape/full screen mode becomes empty after turning the screen off and then on when the system Screen lock is set to None. The keyboard remains active, but it doesn't appear to have focus and no text updates are displayed on the screen. If I tap the Done button or rotate to portrait, then the full screen editor is removed and I can see the previously entered text in the normal EditText view.
I'm reproducing this on JB in both the emulator and a device. The EditText has an android:id and I've found this problem with a variety of EditText and AutoCompleteTextView configurations, but only when the system Screen lock setting is set to None. Everything works fine when it's set to Slide or Pattern, so I suspect that the lack of a screen lock is the source of this problem.
I found the same problem in Calendar, but not in Messaging. When editing an SMS message in landscape/full screen mode and then turning the screen off/on, at first the message appears to be empty, but then it returns after a second. I reviewed the source code below, but I didn't see anything special in onResume() that appeared to be addressing this issue.
https://github.com/android/platform_packages_apps_mms/blob/master/src/com/android/mms/ui/ComposeMessageActivity.java
Does anyone know how to properly fix this, so that when Screen lock is None you can edit text in landscape/full screen mode, turn the screen off and then back on, and the text is displayed just as you left it?
As a (hopefully) temporary fix, I have included the following line in onResume() and it appears to do the trick. Initially I tried to only run this when the IME isFullscreenMode(), but this returns false in onResume() (after returning true in onPause()). I would guess that this is part of the root of the problem. When system Screen lock is None and the screen is turned on, the IME doesn't know that it's in full screen mode when it actually is.
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.restartInput(view);
}

How to set a dialog for portrait only?

Is there a way to showing a dialog always in portrait? I need that because of its layout.
I know that I can set that for an Activity with
android:screenOrientation="portrait|reversePortrait"
But what about a dialog?
At the time of the dialog showing, check the device's orientation.
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
//if orientation is portrait then show, if not then don't.
Not sure these ways will fit your requirement ,
One way is showing Activity as a dialog with screen orientation set to portrait (with dialog theme)
refer DialogActivity from ApiDemos
Other way is forcing activity to change its orientation by
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
and open dialog in onConfigurationChanged() (with some logic).

Categories

Resources