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.
Then right click the app/src/main/res
directory. Choose New > Android Resource Directory.
Select Smallest Screen Width, and then press the >> button.
Type in 600
for the smallest screen width. The directory name will be automatically generated. Say OK.
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.
Right click the directory as before to create the bools.xml
file.
Following Ginny's answer, I think the most reliable way to do it is as follows:
As described here, put a boolean in resources sw600dp. It must have the prefix sw otherwise it won't work properly:
in res/values-sw600dp/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">true</bool>
</resources>
in res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
</resources>
Then make a method to retrieve that boolean:
public class ViewUtils {
public static boolean isTablet(Context context){
return context.getResources().getBoolean(R.bool.isTablet);
}
}
And a base activity to extend from the activities where you want this behaviour:
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!ViewUtils.isTablet(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
So each activity would extend BaseActivity:
public class LoginActivity extends BaseActivity //....
Important: even if you extend from BaseActivity
, you must add the line android:configChanges="orientation|screenSize"
to each Activity
in your AndroidManifest.xml:
<activity
android:name=".login.LoginActivity"
android:configChanges="orientation|screenSize">
</activity>
Here's how I did it (inspired by http://androidblogger.blogspot.com/2011/08/orientation-for-both-phones-and-tablets.html ):
In AndroidManifest.xml , for each activity you want to be able to change between portrait and landscape (make sure you add screenSize - you didn't used to need this!) You don't need to set a screen orientation here. :
android:configChanges="keyboardHidden|orientation|screenSize"
Methods to add in each Activity:
public static boolean isXLargeScreen(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
and: (if you don't override this method, the app will call onCreate() when changing orientations)
@Override
public void onConfigurationChanged (Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (!isXLargeScreen(getApplicationContext()) ) {
return; //keep in portrait mode if a phone
}
//I set background images for landscape and portrait here
}
In onCreate() of each Activity :
if (!isXLargeScreen(getApplicationContext())) { //set phones to portrait;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else {
//I set background images here depending on portrait or landscape orientation
}
The only thing I can't seem to figure out is how to to get the app to change layout files when switching from landscape to portrait or vice versa. I assume the answer is doing something similar to what the above link does, but I couldn't get that to work for me - it deleted all my data. But if you have a simple enough app that you have the same layout file for portrait and landscape, this should work.