Changing layout orientation only for large devices

birraa

An android application I am working on needs to change screen layout only when the screen size is above 7''. I created different layouts for large and xlarge for both land and portrait modes. This seems to have worked well.

I used the following code to determine screen size and change layout accordingly:

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);

  int width = dm.widthPixels, height = dm.heightPixels;
  double dd = Math.sqrt(width * width + height * height)/dm.densityDpi;
  double diagonal = (dd >= (Math.floor(dd) + 0.5) ? Math.ceil(dd) : Math.floor(dd));

  if (diagonal < 7) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);         
  }
}

The problem:
The layout for screens below 7'' is also changed to landscape mode. The above code could not stop the change. Keep in mind that the layout for below 7'' is also created.

What am I doing wrong here?

Muhammed Refaat

There are Two factors here, the first one is your layouts folders and the second one is your code, when you want to represent different layouts for different screens you should use layout, layout-sw600dp, and layout-sw720dp. In this case the layouts in the folder layout is the layouts for the device which is below 7in and thelayout-sw600dp' represent the layouts for devices with minimum size of 7'in and the folder layout-720dp' represents the layouts for the devices bigger that 7in and till the 10`in devices.

The second factor is applying orientation to these layouts, this can happen easily in your onCreate() method using Configuration.class:

    Configuration conf = getResources().getConfiguration();
    if (conf.smallestScreenWidthDp >= 600) { // now it is at least a tablet with 7'in
        if (conf.smallestScreenWidthDp >= 720) { // now it is a 10'in tablet
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); // Or portrait
        } else { // now it is a 7'in tablet
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }
    } else { // now it is a regular device below 7`in
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive