How can I disable landscape mode for some of the views in my Android app?
I was not aware of the AndroidManifest.xml
file switch until reading this post, so in my apps I have used this instead:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed Portrait orientation
A lot of the answers here are suggesting to use "portrait"
in your AndroidManifest.xml file. This might seem like a good solution - but as noted in the documentation, you are singling out devices that may only have landscape. You are also forcing certain devices (that work best in landscape) to go into portrait, not getting the proper orientation.
My suggestion is to use "nosensor"
instead. This will leave the device to use its default preferred orientation, will not block any purchases/downloads on Google Play, and will ensure the sensor doesn't mess up your (NDK, in my case) game.
use
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.co.nurture.bajajfinserv">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
we can restrict the Activity in portrait or landscape mode by using the attribute or android:screenOrientation.
if we have more than one activity in my program then you have freedom to restrict any one of activity in any one the mode and it never effect the others which you don't want.
Add android:screenOrientation="portrait"
in AndroidManifest.xml file.
For example :
<activity android:name=".MapScreen"
android:screenOrientation="portrait"></activity>
Just add this attribute in your activity tag.
android:screenOrientation="portrait"