How to disable terrain on the layout

Im using all possible layouts for my application, portrait and landscape (small-normal-large-xlarge), but after testing on a small screen I just didn’t like what it looks like what I'm trying to do is turn off the landscape for small layouts . Is there any way to do this? All I found is changes to the manifest, but I believe that by reconfiguring the manifest, I applied the changes to all the layouts.

+4
source share
4 answers

The easiest way is to put this in the method of onCreate()all your actions (even better, put it in the BaseActivity class and extend all your actions from it).

@Override
protected void onCreate(Bundle bundle) {
   super.onCreate(bundle);

   if (isLargeDevice(getBaseContext())) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   } else {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }
}

This method can be used to determine if the device is a phone or tablet:

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;
        }
    }
+8
source

Check this link, you can check the device type and set the orientation as needed

Android: allow portrait and landscape for tablets, but force portrait on phone?

0
source

,

:

    <activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

     ///check the screen size and change it to potrait
    }
}

, ,

0

, 480 : oncreate:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
if(width==480){

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


}
-1

Source: https://habr.com/ru/post/1523333/


All Articles