Search code examples
nativescript

Hide Lanscape mode in Android phone in NativeScript Angular


Hide Lanscape mode in Android phone in NativeScript Angular

1.Allow both orientations in Android tablet

2.Restrict Landscape and allow only portriat version in phone


Solution

  • lockOrientation(orientation) {
        const activity = app.android.startActivity;
        switch (orientation) {
            case 'unlocked':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                break;
    
            case 'portrait-primary':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
    
            case 'portrait-secondary':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
    
            case 'landscape-primary':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
    
            case 'landscape-secondary':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
    
            case 'portrait':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                break;
    
            case 'landscape':
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                break;
    
            default:
                activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                break;
        }
    

    just checking and calling the below method is working for me

            if (app.android && device.deviceType === 'Phone') {
                this.lockOrientation('portrait');
            }