SCREEN_ORIENTATION_PORTRAIT not working after installing the app - android

I have a problem to lock the orientation of my app in portrait mode. I added android:screenOrientation="portrait" and android:configChanges="orientation" to my Manifest. Moreover, I added setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) in the onCreate(...) of my MainActivity. The MainActivity is extended by AppCompatActivity. At any time, it works, so you never see your app in the landscape orientation, except when you install it for the first time on your device. Then it is possible to change between landscape and portrait orientation. Does anybody know why this happens? Thank you in advance.

Don't apply the orientation to the application element, instead you should apply the attribute to the activity element, and you must also set configChanges as noted below.
Example:
<activity
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This is applied in the manifest file AndroidManifest.xml.

Related

Definition of android device natural orientation

I want to change the basic orientation of an android device. Curently its startup orientation is landscape, I need portrait.
I managed to modify the acceleration values so that the axis now point to where I want them to point to.
But the device seems to have its own definition of up and right. Now I have to change this.
Can someone tell me where this can be done? I guess there is a config file out there that I can't identify.
There are millions of hits searching for this problems. Unfortunately they all deal with java programming, not with OS code.
In your manifest file under activity tag that is on which screen you want to apply that orientation.
For portrait
android:screenOrientation="portrait"
For landscape
android:screenOrientation="landscape"
Add this lines of code in Androidmanifest.xml for the orientation
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="landscape">

Troubleshoot orientation changing issue

In one of my application, there are multiple Activity(s),
Mostly all activities are configured to be in Landscape orientation by providing android:screenOrientation="sensorLandscape" for each activity
in android manifest file. But one of the activity called 'MyDashBoardActivity' changes orientation to Portrait automatically, when that Activity starts in one of my Android device, but it works fine (Loads in Landscape orientation as expected) in other devices.
How can troubleshoot this issue.
remove this android:screenOrientation="sensorLandscape"
And write In your onCreate :
after setContentView();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Try myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="MyDashBoardActivity"/>

Android always in portrait mode not working

I want to run my application always in portrait mode. I searched and found the code below, then I added to manifest file.
android:configChanges="orientation"
android:screenOrientation="portrait"
but my application still chancing orientation when I press CRTL+F12 on AVD.
How can I force my application always run in portrait mode?
Ps: I tried the code reverse. (Second line first then first line)
Did you add that in the activity element, not the application element?
<activity
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
</activity>

Layout doesn't rotate automatically

Hi I already made two XML layout file with same name, one in layout folder and the other in layout-land.
I already add the configChanges tag in Manifest, like this:
<activity
android:name=".Main"
android:label="#string/title_activity_main"
android:configChanges="orientation|screenSize" >
...
</activity>
The layout won't change automatically after rotating my phone.
But when I started the app in Landscape orientation, it will use the layout-land xml and keep using it even after I rotated my phone into portrait. So, there is no error in the landscape code.
I'm using Android 4.1.2 for testing. Is this the problem?
Or do I need to add some code in the Activity class?
Thanks
[Update]
I tried this in freshly-made project. But the problem still the same
Remove android:configChanges="orientation".
Using this attribute means that you will handle the rotation yourself, but you want the OS to handle it for you.

Locking the screen to landscape for only one activity

I have looked around SO and found how to lock my entire application to Landscape by editing the Manifest file. However, this is not what I want.
I want every activity to be locked portrait, except for one, which will be locked to landscape.
In your manifest file, find your Activity declaration and use the following:
<activity
android:name=".ActivityName"
android:label="#string/app_name"
android:screenOrientation="landscape" >
</activity>

Categories

Resources