I was wondering if it is possible to fix the orientation of a layout based on the position of a device. For example if an app is launched while in a portrait position, the app is fixed in portrait. If the app is launched in landscape the orientation is fixed to landscape.
I understand you can fix the orientation to either portrait or landscape in the manifest but is there a way to fix the orientation dynamically based on the the device position?
I believe what you want is
android:configChanges="keyboardHidden|orientation"
in the manifest. I haven't tested it but I think that tells Android not to handle those changes. Hope this helps!
Related
I tried using the SystemChrome to change the orientation and it works for Lock ScreenOrientation. But As i provided two orientation. Application takes the first orientation until user start the Auto Orientation from Setting.
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
Is it possible to allow user to change the orientation without using Auto-Orientation Service ?
It works only with android devices:
add attribute to main activity in AndroidManifest.xml
<activity
android:screenOrientation="sensorLandscape"
...
and now screen retains landscape orientation, but can be either normal or reverse landscape based on the device sensor. The sensor is used even if the user has locked sensor-based rotation.
Hey #Ashish I found one link for you for Orientation functionality and Concept
SystemChrome.setPreferredOrientation
The idea here is using make a sensor plugin to take only the screen orientation landscape info. Sensor will work even user locks the orientation.
https://www.techrepublic.com/article/pro-tip-use-android-sensors-to-detect-orientation-changes/
From the flutter site, put a listener somewhere and take a rotation programmatically by setPreferredOrientations method.
https://master-api.flutter.dev/flutter/services/SystemChrome/setPreferredOrientations.html
I am trying to prevent myy app orientation to landscape to portrait or vice versa when I lock the screen by clicking screen rotation to off.I saw that once I deselect screen rotation, the entire screen locks up including the screen of the apps I have on my device except for my app where orientation change still triggers landscape portrait recognition even when locked.How do I go around it? DoI need to set something in android manifest to make it work in accordance with the screen rotation ?
Thanks!
You can set either add android:screenOrientation="portrait" or android:screenOrientation="landscape" parameter to your AndroidManifest.xml.
edit:
You can also "lock" screen orientation programmatically using setRequestedOrientation (int requestedOrientation)
If you want to lock orientation when user locks it in the system you need to add android:screenOrientation="user" in your activity tag
In my manifest I've setup an activity restricted to portrait orientation. But I need to remove this restriction on condition. So, how do I achieve removing orientation restrictions programmatically ?
upd: my present settings are:
<activity
android:name=".activity.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="orientation">
/**
* Defines whether the device being used is a tablet and if so adds horizontal orientation option.
*/
protected void _updateScreenOrientationModes(){
if(((MyApplication) getApplication())._isTablet == true)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
Whether or not you've set android:screenOrientation in your Manifest, you can programmatically set the orientation with Activity.setRequestedOrientation().
In essence, "removing the restriction" is accomplished by
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
after which the Activity will exhibit the default behavior of changing screen orientation when the physical device orientation changes.
It's likely, however, that you want to actually match the current physical device orientation at the same time. I haven't tried it, but I'm pretty sure that if all you did was the above, if the device was physically in a landscape orientation when you did it, then you'd stay in portrait mode until you physically moved the device to portrait and back to landscape.
What I would do is be sure to set android:configChanges="orientation" in your Manifest and override Activity.onConfigurationChanged(), in which you can, according to your condition, either perform the orientation change or cache the orientation. Then whenever your condition changes, you'll have the current physical orientation handy so you can change it at that point if necessary.
Programmatically you can change your screen orientations by using "setRequestedOrientation()"
In your java class write the following code as per your condition required.....
To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE constant:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I'm going to leave my other answer, as it addresses the more general question that you asked. However, in a comment to someone else you said:
I need to allow tablets to have both orientations and handsets only portrait
Your particular case is actually easier than the general case: Remove both android:screenOrientation="portrait" and android:configChanges="orientation" from your Manifest to allow the default behavior. Then, during startup, if the device is a handset, force portrait orientation with
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
A device will obviously never change between being a tablet or a handset at runtime, so you only need to do this once, at startup. Tablets will get the default behavior, so whatever physical orientation the device is in, that's what they'll use. Handsets will be forced into portrait and stay that way.
I have an camera activity in my app, where user can make a photo. For some reasons it's fixed to landscape mode(by the screenOrientation in manifest). All works perfectly but I need to know - what screen mode on picture? User can rotate device and try to make a photo in portrait mode.
I've tried to use getRotation method but it returnes only 0 degrees, cause i'm set to portrait screenOrientation.
I want to ask - is there some ways to solve this problem?
Thanks for any help!
Use the orientation sensor, ain't as difficult as you might think.
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
Android phone orientation overview including compass
how can i get the rotation of the device in the four main orientations? and is there a corresponding event that i can capture?
also, is it possible to disable this rotation for my app?
thanks!
Explains how to get the size and orientation of the screen.
http://indyvision.net/2010/02/android-screen-size-orientation/
However, the device already changes its own behavior depending on the device orientation. For instance you can specify screen layouts dependent on the orientation, one for vertical and one for horizontal.
To prevent the Activity from rotating with the device, you can add android:screenOrientation="portrait" or "landscape" to AndroidManifest.xml for each Activity declared in your app.