How to distinguish between Landscape (Right/Left) vs Portrait (Up/Down) on android? I tried:
getResources().getConfiguration().orientation
and it always returns Configuration.ORIENTATION_PORTRAIT.
also, I checked:
How to detect landscape left (normal) vs landscape right (reverse) with support for naturally landscape devices?
Check orientation on Android phone
I tried the onConfigurationChanged as well. I cannot get Right vs Left on landscape mode (up vs down on portrait).
Is there a clean way of finding this on Android 5.0+?
Note: My activity could be launched with any orientation. Capturing orientation changes using a listener may not be viable since I want the real orientation of the phone (even before I get a callback on orientation changes).
Also, by default:
android:screenOrientation="portrait"
Note: My activity responds to auto-rotate changes.
Related
I have set orientation programmatically to landscape for tablets and portrait for phones.
The issue is that when navigating on tablet from one activity to another, with auto-rotation disabled and in portrait mode the screen is flickering. First, it navigates to the new activity in portrait mode and then changes to landscape which is not the output I would want.
I've tried adding to the manifest
android:screenOrientation="locked"
android:screenOrientation="nosensor"
android:screenOrientation="sensor"
and in activity for tablets I've set
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Which doesn't flicker, but still its not what I want.
Expected outcome is for phones to stay as portrait mode and tablet as landscape. I think that the main issue is with system auto-rotate setting as disabled, because when it's enabled and I try to navigate from one activity to another, the screens doesn't flick and just stay as either portrait or landscape.
I think I could use
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
but then I need settings write permissions, which should be enabled by the user and I want to know if somehow I can omit that
The question sounds way harder than it is. Simply all I want to do is to be able to set a different layout for, I already added a landscape orientation, and recreated the whole design the way I desire in landscape. However, the app restarts each time I rotate the phone, if I add android:configChanges="orientation" in manifest, it does save the state but not the way I set in landscape. The app is 4 layouts in each orientation. So if I could just make different constraints for landscape the doesn't interrupt the original portrait it could solve my problem.
sorry for the poor explanation. It's my first week with Android.
You are correct in designing different layouts for different orientations.
For the state, you should override the onSaveInstanceState() method in Java.
Using that, you can save the state of the application and then inflate the layout the way you like in Java.
The first activity(locked portrait orientation) has 2 buttons that both open the second activity but A button opens it in portrait and B button opens it in landscape orientation.
The problem is that when B button opens the second activity (which is in dialog configuration and the first activity is shown in the background) the first activity changes orientation with the second one, thus restarting.
The question is, A) Can i prevent first activity from changing orientation and B) Can i disable orientation change animation so that the screen doesn't look like turning from portrait to landscape but just becoming landscape instead?
P.S For the A) question i know how to use saved state but i want to avoid it.
Use Below Code in Manifest:
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
Edited:
Sorry there is no way to control the rotation animation. This is done way outside of your app, deep in the window manager where it takes a screenshot of the current screen, resizes and rebuilds the UI behind it, and then runs a built-in animation to transition from the original screenshot to the new rebuilt UI. There is no way to modify this behavior when the screen rotation changes
When I click a button in my App, the screen orientation will automatically go to landscape :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The screen get to landscape successfully but it caused my onConfigurationChanged not working anymore.
I tried to use: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); but it makes no difference.
What should I do, to make the screen go to landscape, when I click the button, without turning off the onConfigurationChanged?Or do you have any other ways to accomplish this?
If you are using API13 or higher as your target,
in your manifest you need to add ScreenSize to your configChanges:
android:configChanges="orientation|screenSize|keyboardHidden"
That should do the trick!
I am trying to honor the android rotation lock setting - I want my app to display in whatever orientation the home screen is locked in. I found the following knobs:
Settings.System.ACCELEROMETER_ROTATION - determine if the screen orientation is locked.
Display.getRotation() - determine if the device is rotated from it's "natural" orientation, which may be portrait or landscape.
Display.getWidth()/getHeight() - determine the current orientation of the device.
I thought this would be simple to do, but I can't seem to find the right combination of the above to get this to work. The reported values seem inconsistent, especially during app startup (is onResume the right place to check these values?).
Note I don't want to simply always fix the orientation of my activity to portrait or landscape, I want to set it to the orientation the user has locked the screen in. For tablets this could be landscape or portrait. If the user sets the Auto-Rotate setting, I want my activity to operate in sensor mode.
after hours of debugging, it appears the answer is as simple as checking if rotation is locked and if so, calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR). Now excuse me while I go step off the nearest bridge...