I want my view to launch in landscape mode. But once the user changes to landscape orientation and come back to portrait, I want my view to behave normally and change orientations according to device orientation.
Basically. view should launch in landscape only for the first time and behave normally after that.
Write at onStart of your Activity
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(YourActivity.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(YourActivity.SCREEN_ORIENTATION_PORTRAIT);
Related
I'd like to have all activities in portrait orientation.
So I have tried both
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
in onCreate(), onPause() or onResume() but the screen will flip to landscape and back to portrait in case of I rotate device in landscape from the beginning before opening the app.
But if I remove setRequestedOrientation method and just add android:screenOrientation="portrait" in AndroidManifest.xml only then it works (but let's imagine if I have 30 activities so it's better to have in BaseActivity instead)
How to reproduce
BaseActivity has method in onCreate(), onResume(), onPause() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Rotate device in landscape orientation.
Open app, Activity A is flipping from landscape to portrait.
Start Activity B from Activity A, Activity B is flipping from landscape to portrait.
Note
I have checked already
Android 8.1 screen orientation issue: flipping to landscape a portrait screen
Activity rotating itself and back to normal in android 8.1
but it didn't work (it works only if orientation is defined in AndroidManifest)
If someone knows how to fix please share the solution
Thank you so much.
I have an activity, let's call it HomeActivity, whose screenOrientation has been set to landscape in the manifest file.
Clicking on a button in the HomeActivity presents a Dialog Activity which floats over HomeActivity.
There is a portrait version and a landscape version for the layout that is to be displayed in this dialog, but for this scenario, only the portrait version should be displayed.
In the onCreate() method of the Dialog Activity, I've requested Portrait Orientation but this causes both the dialog and the HomeActivity behind to rotate to portrait mode. I do not want this.
Is there a way to set the orientation of the top most activity without affecting the orientation of the "behind" activity?
Thanks for your help?
Orientation is system-wide property so it can only be landscape or portrait at the one time. This includes all displayed activities (including dialog), status bar and buttons.
You have to display HomeActivity and Dialog Activity with the same orientation.
Also, forcing Activity orientation in the manifest is a bad practice in general.
I have one activity which i am using as host for calling all my fragments.I am using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) for setting screen in landscape mode.But problem is that it is making every fragment in landscape mode because their is only one parent activity for all of them.I can not use android:screenOrientation="landscape" because this will also work the same.
I want one of my fragment to be displayed always in landscape mode and remaining fragment as according to device orientation.Is their is another ways for doing it?
If you wanna show one specific Fragment in landscape you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in the onCreate() of your Fragment.
In this Fragments onPause() f.e. you can call
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
which is the default value and enables screen rotation changes.
I've a problem trying to capture the onConfigurationChanged event. This is the scenario:
Activity A starts (listens to onConfigurationChanged)
Phone rotated to landscape mode (onConfigurationChanged being called). Start activity B.
Activity B starts (listens to onConfigurationChanged) (LANDSCAPE)
Activity B rotates back to portrait (onBackPressed event raised). Activity B is destroyed and A is called back.
Activity A resumes
Phone rotated to landscape mode. The onConfigurationChanged is not called this time.
Phone rotated to portrait mode. The onConfigurationChanged called.
Phone rotated to landscape mode. The onConfigurationChanged called.
Why step 6 don't call onConfigurationChanged event? it doesn't make sense at all. Do you know what could be the issue?
I have the same problem, I update UI onResume() to fix it.
I had exactly the same issue. I still don't understand this behavior of android system, but you can use onOrientationChanged of OrientationEventListener instead of configuration change handling.
See this answer (example is not perfect but shows the way): https://stackoverflow.com/a/13844242/554281
I have faced same problem and was stuck in this for more than a week. Then I have prepared a sample APP with tabHost and reproduced the same issue on the sample app. After playing around with that, I found that this is a bug of android's tabHost. So, I have migrated tabHost to FragmentTabHost and found the problem is gone.
The simulation of the Issue with tabHost:
Let, there are two tab i.e A and B
Simulation 1 (Arise the bug)
Arrive on A in portrait mode
Go to B on portrait mode
Rotate B to landscape
Back to A on landscape mode.
Rotate A to portrait (onConfigChanged method of tabActivity gets fired. But onConfigChanged of A activity doesn't get fired)
Simulation 2 (Works fine)
Arrive on A in portrait Mode
Go to B on portrait mode (continue rotating as you want, but stop on portrait mode)
Back to A on portrait mode.
Rotate A to landscape (both onConfigurationChanged method of tabActivity and A activity gets fired)
Summary: If you get back to the screen in the same orientation you got out, both onConfigurationChanged() will get called.
I use in the AndroidManifest:
<activity android:name="Zoom" android:text="#string/Name" android:screenOrientation="landscape"></activity>
to force this specific Activity(B) to be been see as landscape, my problem is when i am moving to the next Activity(c) or back to the Activity(A) its still show as landscape
what i should do different??
thanks for helping!!
If your device is in landscape orientation, and you're not specifically setting orientation for the other activities, then they will show in landscape orientation.
Force the other activities to portrait with android:screenOrientation="portrait".
This will mean if your device is in landscape and you switch activity, the new activity will display in portrait (on its side) and the user will then have to rotate the phone into portrait mode to view it correctly