Prevent Android from changing orientation on specific devices

I know that you can limit the orientation change in the manifest file for your Android application, but I was wondering if there is a way to do this depending on the type and size of the device.

I would like to prevent it on small / medium phones, but allow it on large phones / tablets.

What is the best way to achieve this? Any help would be greatly appreciated.

+5
source share
5 answers

For this, I think you will need to collapse two things in one.

  • First choose the screen size of the device
  • And then, based on the result, turn orientation on or off.

In 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();
}

: fooobar.com/questions/45242/... ( SO)

, , , :

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
+6

, , , .

, .

if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) 
{     
    // screen is large.. allow orientation changes
}

else
{
       //restrict orientation 
}
+1

onCreate setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ( , ).

, , , .

0

setRequestedOrientation();

0

You need to rewrite the public void onConfigurationChanged(Configuration newConfig)and create your own logic to check the density and size of the screen.

But be careful .. this is not a good practice.

0
source

All Articles