So the general consensus is this; most people use their phones in portrait and most people use their tablet in landscape. Depending on which activity it is my app's layout goes crazy when you rotate on the phone to landscape and it just wouldn't be worth the time to fix considering users are unlikely to rotate here and have no reason to do so. I'm aware of the ole orientation="portrait" trick in the Manifest in the activity element, however this locks tablet users into portrait which wouldn't be appropriate. I would like to disable portrait on all my activities for tablet users yet simultaneously disable landscape on most all my activites for phone users. I tried to pull a fast one by making a layout-large-land folder and no layout-large folder, but that doesn't prevent the orientation from changing on tablets.
-
please check this link this is really helpful to you. https://stackoverflow.com/a/14793611/7893686 – Bhupat Bheda May 30 '18 at 05:24
-
This solution worked for me: https://stackoverflow.com/a/60381441/7826494 – David Elmasllari Sep 14 '21 at 15:05
4 Answers
I guess you can use a code like this in onCreate()
method:
int screenLayoutSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
if (screenLayoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || screenLayoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
And don't specify any orientation in xml, so by default it switches in both mode.

- 8,839
- 1
- 18
- 53
-
There are dozens of answers out there regarding this specific question, and yours is the only one that really works (and makes sense). – nibbana Mar 12 '19 at 15:00
-
Indeed this is a much cleaner way than "hacking" the XML files. This should be the accepted answer. Works just fine for me! – alexkaessner Aug 21 '19 at 19:21
-
Note that while this works, it is still possible for your app to temporarily appear in landscape orientation (seems to occur when the device has been rotated to landscape between the Activity pausing and resuming), so if you're doing programmatic layout, you'll need to account for the brief appearance in landscape mode. – Jason McClinsey Sep 18 '19 at 16:33
-
1RN developer here. It worked. I added it in MainActivity.java > onCreate method – Erik Rybalkin Jan 18 '21 at 16:32
If you just use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
from code you will catch interesting effect on Android 26+. If the system autorotation option is enabled and you hold phone in landscape orientation and start new Activity it will appears in landscape orientation and then rotate to portrait in a few seconds. You doesn't catch such effect if set android:screenOrientation="portrait"
option in the AndroidManifest. But there are not way to have different rotation option into AndroidManifest for phone and tablet.
There's way to solve that if you wish lock portrait orientation on phone and unlock autorotation on tablet.
Set option android:screenOrientation="locked"
in the AndroidManifest for each Activity in you project.
<activity android:name=".SomeActivity"
android:screenOrientation="locked" />
where "locked" – Locks the orientation to its current rotation, whatever that is. Added in API level 18 from Android docs
Then set in parent BaseActivity such code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
int orientation = getResources().getConfiguration().orientation;
if (isTablet()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
} else if(orientation != Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
There are a few ways to detect that current device is Tablet. Choose implementation of isTablet()
method yourself.

- 890
- 12
- 20
-Do One thing put this on the in the res/values file as bools.xml or whatever (file names don't matter here):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
</resources>
and Put this one in res/values-sw600dp and res/values-xlarge:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
</resources>
and then into java class file write this below code in onCreate method:
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Devices that are more than 600 dp in the smallest width direction. see the below link for the how to add directories and file into android studio project

- 1,349
- 12
- 19
My suggestion would be to first find the way to know at run-time whether the activity is being executed in a Tablet by invoking a resource as explained in this answer. Then set the orientation as explained in this answer.

- 3,349
- 2
- 14
- 22