I m building an app which an a main layout in portrait format
I locking orientation with
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
However, I need to detect screen orientation changes anyway because I want to load another Activity if the screen is rotated.
How to do that ?
I tryed onConfigurationChanged but it s never called.
Some things to look at if onConfigurationChanged isn't firing:
When you declare the manifest, make sure you have defined at least orientation and screenSize for the activity configChanges. Like this:
<activity android:name="com.test.act" android:configChanges="orientation|screenSize">
Make sure the device doesn't have rotation blocked. For example if you are using a Nexus 7:
http://www.howtogeek.com/120056/how-to-enable-landscape-orientation-on-the-nexus-7s-home-screen/
Related
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
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.
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 activity which must always be launched in landscape mode. In my onCreate() method, I use the dimensions of the screen (width and height) to set up the interface, so it is important that the activity is not initially created with the wrong orientation.
To do this, I have the following in the manifest:
<activity android:name="app.myapp.WideActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
This seems to work completely fine on my phone, but on my tablet (samsung galaxy tab, android 3.1) the activity is launched in portrait mode and switches orientation almost immediately (after the onCreate() method - it seems). On my phone (android 2.3) the screen is already landscape before the onCreate() method is called.
Putting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in the onCreate() method does't seem to help. How can I achieve what I'm trying to do?
I am not sure if you can fix the exact problem. It seems for me the problem is in samsung's implementation of activity/window management framework part.
However, you can try to prevent a crappy UI to appear by delaying the rendering.
In onCreate() show some default layout - just a plain black layout stretched to the entire screen.
In onResume() check the screen orientation, if it is not landscape then you post a delayed runnable which will apply your layout (setContentView(my_UI)).
Inside the runnable you also check if screen orientation is landscape, and re-post the runnable if orientation is still portrait, otherwise set the UI.
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.