Android: allow portrait and landscape for tablets, but force portrait on phone?

Here's a good way using resources and size qualifiers.

Put this bool resource in res/values 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>

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>

See this supplemental answer for help adding these directories and files in Android Studio.

Then, in the onCreate method of your Activities you can do this:

    if(getResources().getBoolean(R.bool.portrait_only)){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

Devices that are more than 600 dp in the smallest width direction, or x-large on pre-Android 3.2 devices (tablets, basically) will behave like normal, based on sensor and user-locked rotation, etc. Everything else (phones, pretty much) will be portrait only.


You can try this way first get the screen size of the device

if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
    Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();

}
else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
    Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();

} 
else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
    Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
}
else {
    Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}

and then set orientation according to that

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Supplement to the accepted answer

You can do the following steps in Android Studio to add the res/values-sw600dp and res/values-large directories with their bools.xml files.

values-sw600dp

First of all, from the Project tab select the Project (rather than Android) filter in the navigator.

enter image description here

Then right click the app/src/main/res directory. Choose New > Android Resource Directory.

Select Smallest Screen Width, and then press the >> button.

enter image description here

Type in 600 for the smallest screen width. The directory name will be automatically generated. Say OK.

enter image description here

Then right click on the newly created values-sw600dp file. Choose New > Values resource file. Type bools for the name.

values-large

Adding a values-large directory is only necessary if you are supporting pre Android 3.2 (API level 13). Otherwise you can skip this step. The values-large directory corresponds to values-sw600dp. (values-xlarge corresponds to values-sw720dp.)

To create the values-large directory, follow the same steps as above, but in this case choose Size rather than Smallest Screen Width. Select Large. The directory name will be automatically generated.

enter image description here

Right click the directory as before to create the bools.xml file.