Good day!

My application consists of one functional Activity, in which the change of content consists in the change of Fragments. There is almost nothing in the Layout file of the Activity itself (Toolbar, download icon, FAB), all main elements are located in Layout fragments. Now directly a question. Is it possible to somehow make it so that:

  1. On devices of the same size (in my cases small and normal ), the orientation change was prohibited (more precisely, only portrait portrait was allowed) ...

  2. ... but on a different size device ( large and xlarge ) was the orientation change allowed ? Or, as an option, only land orientation is allowed.

Any method will do, although it is desirable, of course, declarative (via the manifest or project files). Well, preferably without crutches on the type of "software orientation change in OnCreate when a device of some size is detected."

  • 3
    Possible duplicate question: Different screen positions for different devices - Werder
  • @Werder yes, duplicate (honestly, I did not find it when I searched). But here they gave a much more convenient and high-quality solution that was proposed by xkor. - Janislav Kornev

2 answers 2

The option with setRequestedOrientation in onCreate has one unpleasant effect. If the user kept the device in the orientation that you want to disable, then when the application starts, despite the call to setRequestedOrientation , it will have time to create an activation with the layout for the current, and not the desired configuration, and if you have it, it is very different between the configurations and you will start working with it as with the right layout, it’s possible to crash. Well, even without crash it will be visually ugly that at first the application will appear in the current orientation and immediately turn over to the correct one.

Through the manifest, unfortunately, we cannot make different orientations for different types of devices, but we can cheat a little. You can inherit from your main activity (we will call it BaseMainActivity) the other two (let's call them PhoneMainActivity and TabletMainActivity), in which we will not do anything new, they are only needed to prescribe different orientations for them. We also need a separate activit with which the application will run (let's call it SplashActivity), you can show a boot image on it, or close it immediately on onCreate , the main thing is to check the device type and, depending on it, run PhoneMainActivity or TabletMainActivity .

    In onCreate your Activity :

     @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); if (isLargeDevice(getBaseContext())) { // land или портретная this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); // если надо только land this.setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else { this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } 

    Well, the verification method:

     private boolean isLargeDevice(Context context) { int screenLayout = context.getResources().getConfiguration().screenLayout; screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK; switch (screenLayout) { case Configuration.SCREENLAYOUT_SIZE_SMALL: case Configuration.SCREENLAYOUT_SIZE_NORMAL: return false; case Configuration.SCREENLAYOUT_SIZE_LARGE: case Configuration.SCREENLAYOUT_SIZE_XLARGE: return true; default: return false; } }