My activity forces portrait mode using android:screenOrientation="portrait" in the manifest. However, some of my users (on tablets that are by default in landscape mode) have asked me to make this a configurable option. Is it possible to set the orientation in code (without triggering an orientation-change animation, so before the activity is actually displayed)?
Yes, simply call:
Activity.setRequestedOrientation (int requestedOrientation)
Taken from
http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation%28int%29
If you want to lock the screen orientation after setting it, take a look at this post:
Screen orientation lock
Related
I made an app using accelerometer but problem is whenever it detects rotation of the screen, it goes to main menu.
I used following code to disable rotation but it still detects the rotation action and goes to mainmenu although it does not become landscape mode anymore.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
How can I prevent this rotation detection?
Put android:screenOrientation="portrait" inside your activity tag in the AndroidManifest.xml file. That attribute defines that this specific activity should always be run in the portrait mode and thus Android will not try to change screen orientation on rotation.
You will also want to add
android:configChanges="keyboard|keyboardHidden|orientation"
in order to prevent restarts when the keyboard appears or is hidden.
If you use this, you can even remove android:screenOrientation="portrait" if you want your game to work in both landscape and portrait mode. See the libgdx wiki: https://code.google.com/p/libgdx/wiki/ApplicationConfiguration#The_.xml_File
Kimi answer is correct, but if you want to run your game also on the Amazon device, then you should add the additional value "screenSize". So result would be:android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
I am using the following line of code for each of my activities in order to force the screen orientation to portrait mode:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
It works ok unless I put the device in landscape mode before launching the app. In this case, the activity closes suddenly.
Any idea on how to manage this case?
you need to use android:screenOrientation = "portrait" in your activity tag.
Is it possible in Android to capture screen rotate events but not actually have the activity respond to the rotate events?
To make things more difficult, I've got an activity backed by a TabActivity, and in one of the tabs I want to rotate some of the content area on screen - but leave the tab in portrait mode.
I've tried setting the activity orientation to portrait in the manifest, but then I no longer get the onConfigurationChanged(..) events. However, If I remove this line, the whole tab rotates as well.
Any help appreciated!
You might want to add "orientation" to activity's configChanges property in AndroidManifest.
android:configChanges="orientation"
http://androidappdocs.appspot.com/guide/topics/manifest/activity-element.html#config
Set the orientation to portrait mode in the manifest as you did, but listen directly to the accelerometer sensor (which is the sensor from which Android derives the orientation of the device) updates and parse them yourself to know when the orientation changed.
It's actually quite easy to do. See here for an example: http://www.anddev.org/accessing_the_accelerometer-t499.html
I am displaying Timer on a screen .
After starting the Timer , I am changing the orientation from the portrait to landscape mode. In order to avoid the timer getting reset when changing from the portrait to landscape mode , I am adding the following code to the activity declaration in the manifest:
android:configChanges="orientation"
The problem I am facing is that , when the screen is switching from the portrait to landscape mode , the layout which gets displayed is same as portrait & not in landscape mode. Only a part of the screen of the landscape orientation is being occupied.
Is there any way by which we can avoid resetting the timer from changing from the portrait to landscape mode & also have a proper landscape layout?
Kindly provide your inputs.
Thanks in advance.
There is a simple way of doing this.
if you have two different layouts for landscape and portrait then let the android handle all the stuff for you. i mean do not override methods onConfigurationChange() method unless strictly required and do not add android:configChanges="orientation". just make different folders for portrait mode and landscape mode. viz. layout and layout-land....
To save present state of views use bundle. whenever orientation changes android reload activity and call onCreate() method. This saved bundle is passed in onCreate() method. Now you can retrieve views' state from this bundle.
now next question will be how to use this bundle. Then here is the quick example.
override onSavedInstanceState() method to save bundle.
Thanks.
Is there any way by which we can avoid
resetting the timer from changing from
the portrait to landscape mode?
Yes, store the value somewhere during onPause and read it during onResume.
Also have a proper landscape layout
Read up on Providing Resources, in particular table 2's "Screen orientation" section which also links to a very nice page about handling runtime changes.
All you need to do is make the manifest file set the screenOrientation="landscape"
<activity android:name=".main"
android:label="#string/app_name"
android:screenOrientation="landscape">
<intent-filter>
That turns off the auto sensor on only the form that you name to have landscape.
I'm currently working on an Android application that have different behaviors according to the screen orientation.
I'm using getWindowManager().getDefaultDisplay().getRotation() to get the screen orientation in the onCreate() method and manage from there.
Basically, my problem is that I need a control over the orientation. In landscape mode, when the user decides it, the application must switch to portrait mode, and stays like that, but only until the use goes to portrait mode, and then again to landscape.
I'm using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to change the orientation when in Landscape. The problem is, using this, I can not go back to landscape.
If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) in onCreate(), the problem is that is switches back to landscape mode after a few seconds (since the screen is still in landscape).
Is there any other way to manage this? Basically, is there a way to catch the screen orientation change with a listner (or anywhere else than in onCreate, since it's not called when the views is locked in portrait mode)? I can't find anything about that...
Any idea is welcome!
Thanks!
Why not try creating two layouts, one in layout-land and one in layout-port. These will automatically be applied when the device orientation changes.
Inside each of these, create two distinct child layouts one for a portrait view, and one for a landscape view and change the visibility of these to reflect the user's preference.
So, you don't need to handle the rotation change at all, let Android manage that for you, and you just manage the user override.