I know you can restrict orientation change in manifest file for your Android application, but I was wondering if there is a way of doing that depending on the device type/size.
I would like to prevent it on small/medium phones but allow it on large phones/tablets.
What is the best way to achieve that? Any help would be much appreciated.
Open your device's Settings app. . Select Accessibility. Select Auto-rotate screen.
Prevent Activity to recreated Most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.
For that, I think you will need to roll up two things in one.
For the first part:
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
Credit: https://stackoverflow.com/a/11252278/450534 (Solution was readily available on SO)
And finally, based on the result of the above code, either of these:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Well, I might be wrong, but as far as I know there is no direct way of doing this.
You need to check the screen size programmatically and on the basis of that, allow or disallow orientation changes.
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
{
// screen is large.. allow orientation changes
}
else
{
//restrict orientation
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With