Android: Allow Portrait and Landscape for Tablets, But Force Portrait on Phone

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.

How to allow landscape for tablets, but prevent it on mobile phones?

You can try...

final data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
if(data.size.shortestSide < 600) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

It worked for me. I hope it helps someone else

Android - Lock phone to portrait but allow tablet to be both landscape and portrait

If your landscape layout is just for tablets, I would suggest you put it in the tablet layout folders ie: layout-sw600dp-land (for 7in tablet landscape) and layout-sw720dp-land (for 10in tablet landscape). This way, the tablet layout will only be used in tablets. If you do not declare a landscape layout for the phone, it will use the regular portrait layout. Separating layout folders for the tablet and phone layouts helps for organization.

Portrait for phone, landscape for Tablet (Android-Layout)

Setting a particular orientation based on device density may not work because there are phones which have higher densities than tablets.

What I did was to disable the device's orientation sensor by setting the attribute in the activity tag in the manifest file like this:

android:screenOrientation="nosensor"

When you run your app, by default portrait orientation is set for phones and landscape for tablets(and hence it'll select the xml file from layout-xlarge-land). And since you've set an orientation lock, it remains in this orientation.

How do I make it only landscape for tablets and only portrait for phones?

Here is my suggestion:
First try to determine the type of device using screen dimensions. You could refer to this post.
On the second step you can change the screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);. There is another post here.

Android : How set force device orientation by device type (tablet, phone)?

Use this in your main activity(Launcher activity) or in Application class,

if (isTablet(mContext))
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

landscape mode in tablet only

You can use a SuperClass for activities and implement this https://stackoverflow.com/a/9629127/710162 on, for example, onCreate().

Set portrait for phone but both portrait and landscape orientation for tablet

You can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ine the onCreate method of your Activity if the device is a phone. To determine if the device is a phone or a tablet, you can have a look at this question.

forcing landscape mode for tablets and portrait for mobiles

I have finally found the solution to my problem. I was not giving a reference to the alternative screen views inside my value resource files. When i edited my bool.xml resource file as you see below, i got the expected result.

bool.xml (mobile)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
<bool name="landscape_only">false</bool>
</resources>

// bool.xml (sw600dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
<bool name="landscape_only">true</bool>
</resources>

// bool.xml (sw720dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
<bool name="landscape_only">true</bool>
</resources>

With this, my activities now start in the requested screen orientation as required.



Related Topics



Leave a reply



Submit