android device rotation - android

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.

Related

Allow user to Landscape Right and Left without using Auto Rotation

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

How do I prevent my app from rotating /prevent the orientation switch once I lock the screen?

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

WebView rotates even if manifest declares that activity will handle orientation changes

I have a WebView embedded in a Fragment. In the manifest file, I have declared that the activity will handle orientation changes:
android:configChanges="orientation|screenSize"
and in the Activity, I have over-ridden onConfigurationChanged() in order to capture the orientation.
I thought this means that we have to explicitly take care of any changes in the screen orientation. But what I see is that the screen is still rotated (although the activity is not re-created).
If I use the following line:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
it does prevent the screen from being rotated, but I don't get the rotation event.
So, in short, I don't want the system to rotate the screen, and at the same time, I want to get an event from the system that the orientation has changed from portrait to landscape.
Thanks,
Rajath
If you override onConfigurationChanged(Configuration newConfig), you should be able to handle the changes.
Thanks for editing the question, what you're after is now clear to me. I've got two suggestionsScreen orientation (i.e. portrait, landscape, reversePortrait, reverseLandscape, etc) just depends on orientation of the device in 3D space. So one idea is to capture the 3D orientation of the device yourself, which is the same information that the operating system uses to make the screen orientation decision. This means that you need to capture the accelerometer and the magnetic field sensor readings. One example of capturing that information is in my answer to Android Compass that can Compensate for Tilt and Pitch.Alternatively, you might try setting up a dummy activity that exists purely to capture the screen orientation information. That activity could sit on the activity stack behind your main activity. Although I'm not sure whether activities that aren't on top of the activity stack are notified of screen orientation changes.

Lock screen orientation in android

I would like for my activity not to rotate when the device is turned.
Using
android:screenOrientation="nosensor"
does disable orientation changes, but with one caveat: the activity switches to portrait mode. I just want it to keep the current orientation (e.g., if the screen was in landscape when the activity was started, then stay in landscape mode even when the device is rotated). This is not what "nosensor" seems to be doing. It seems to simply be the exact same behavior as "portrait". Am I using it wrong?
I've tried using setRequestedOrientation( getRequestedOrientation ), but if the current requested orientation is undefined, then my activity is going to rotate. I just want too "lock" the effective screen rotation.
What you can do is to tell Android that you are going to handle the orientation configuration change on your own. You do it by specifying orientation for the android:configChanges attribute of the activity tag.
<activity android:name=".MyActivity"
android:configChanges="orientation"
android:label="#string/app_name">
See this link for more information.
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
As to why nosensor would not work, is because it's mentioned as below in the documentation.
The orientation is determined without reference to a physical
orientation sensor. The sensor is ignored, so the display will not
rotate based on how the user moves the device. Except for this
distinction, the system chooses the orientation using the same policy
as for the "unspecified" setting.
and as to what unspecified is in the documentation
The default value. The system chooses the orientation. The policy it
uses, and therefore the choices made in specific contexts, may differ
from device to device.

Screen Orientation of Android

Which is more appropriate to use in Screen Orientation.
Declaring <activity android:screenOrientation="sensor" />
or
the long process of creating a new folder layout-land & a new layout? and why? I'm using the first
one now and seems it has the same function as the latter. Thanks!
I think the two are different. When we use android:screenOrientation we're telling android what orientation we want to be displayed in. Having two layout's, one for portrait and another for landscape helps us in cases where we want to layout our views such that it takes advantage of the increased width. If you don't think if need this then just use a single layout. Android will use the same for both orientations.
If you want to prevent android from displaying your view in a particular orientation, etc you can use the former.
sensor just returns the orientaton of the device. It does not optimize the layout like using layout-land would.
From http://developer.android.com/guide/topics/manifest/activity-element.html#screen
The orientation is determined by the device orientation sensor. The
orientation of the display depends on how the user is holding the
device; it changes when the user rotates the device. Some devices,
though, will not rotate to all four possible orientations, by default.
To allow all four orientations, use "fullSensor".

Categories

Resources