Jaydeep Kataria
Jaydeep Kataria

Reputation: 867

Ionic 2 : Screen Orientation like portrait and landscape in android

when i rotate the screen in both direction portrait and landscape but android application of ionic 2 is not rotate

config.xml

<preference name='orientation' value='portrait'/>

app.component.js

import { ScreenOrientation } from '@ionic-native/screen-orientation';

initializeApp() {
this.platform.ready().then(() => { 
if (this.platform.isPortrait) { this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE); } else {  this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT); }
    this.statusBar.styleDefault();
    this.initPushNotification();  
}); 
}

Upvotes: 0

Views: 3550

Answers (2)

HEMANT PATEL
HEMANT PATEL

Reputation: 288

First install ..

ionic cordova plugin add cordova-plugin-screen-orientation

npm install --save @ionic-native/screen-orientation

app.component.ts

 import { ScreenOrientation } from '@ionic-native/screen-orientation';

 constructor(private screenOrientation: ScreenOrientation) { }

 // set to landscape

this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);

// allow user rotate

this.screenOrientation.unlock();





  or 

   constructor(private screenOrientation: ScreenOrientation) {
    this.screenOrientation.lock('portrait');
   }

Upvotes: 2

Jaydeep Kataria
Jaydeep Kataria

Reputation: 867

i found the answer of this question, when phone screen orientation change application also rotate with phone orientation.

first, remove orientation preference from confix.xml

then this code write in app.component.js

app.component.js

if (this.platform.is('android')) {        
    this.ScreenOrientation.onChange().subscribe(() => {

      if (this.platform.isPortrait) {
        this.ScreenOrientation.unlock()
      }
      else {            
        this.ScreenOrientation.lock(this.ScreenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY)
      }
    });
  }

Upvotes: 1

Related Questions