0

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();  
}); 
}
2
  • what is your question? Commented Jun 28, 2017 at 6:16
  • i want to rotate the application with phone's screen orientation Commented Jun 28, 2017 at 6:25

2 Answers 2

2

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');
   }
1

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)
      }
    });
  }
1
  • Is it possible for a single page ?
    – mnhmilu
    Commented Oct 23, 2017 at 8:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.