Search code examples
sapui5screen-size

SAPUI5 get the current screen size


At a specific point of my application I need to determine the current screen size (Desktop or Tablet). At least I would need the screen width but the sap.m.ScreenSize would be better.

How can I do that? Thanks in advance!


Solution

  • function sizeChanged(mParams) {
        switch(mParams.name) {
            case "Phone":
                // Do what is needed for a little screen
                break;
            case "Tablet":
                // Do what is needed for a medium sized screen
                break;
            case "Desktop":
                // Do what is needed for a large screen
        }
    }
    
    // Register an event handler to changes of the screen size
    sap.ui.Device.media.attachHandler(sizeChanged, null, 
    sap.ui.Device.media.RANGESETS.SAP_STANDARD);
    // Do some initialization work based on the current size
    
    sizeChanged(sap.ui.Device.media.getCurrentRange(sap.ui.Device.media.RANGESETS.SAP_STANDARD));
    

    I got this code from:

    https://sapui5.hana.ondemand.com/1.54.8/#/api/sap.ui.Device.media

    there u will also get a more detailed description of how it works