[Solved] Detect screen orientation using Capacitor

Hey all

I am having an issue in Capacitor whereby I can’t detect when the device orientation has changed. I have tried using the Motion plugin (https://capacitor.ionicframework.com/docs/apis/motion/), but don’t seem to be getting it right.

The plugin continuously fires, but it does not return if I am in landscape or portrait mode.

My use case is that depending on which mode the user is in, it will update the slides per view accordingly on an ion-slide.

Please see the code that I am using (as stated in the docs):

Motion.addListener('orientation', (event: MotionOrientationEventResult) => {
    console.log(event)
})

Does anybody know how to use this plugin to determine the screen orientation of can you point me in the right direction?

Regards,

English and JavaScript are both horrendously-designed languages that nobody should have to use ever, each with one of their extremely scare marginally-redeeming qualities being prevalence. I think you got bit by bugs in the former this time. I think those docs mean “orientation” in the sense of “are we pointing north or south”, while it sounds like you’re looking for “portrait versus landscape”. If that’s the case, I would look here instead.

2 Likes

Thanks for the response @rapropos, I am indeed looking for portrait vs landscape.

One question however, can one use Cordova plugins in a Capacitor project? This is my first capacitor project, so I am still learning it’s ins and outs and am not aware of being able to do that…

Many of them, yes.

Sweet, thanks a mil :facepunch:

Your wit, or your code
I don’t know which is more satisfying