I'm new to Android and making some toy apps to try things out for my own amusement.
I want to make an app that starts in whichever orientation the user is holding the phone but then stays in that orientation no matter which way the phone is rotated.
I'm experimenting with the android:screenOrientation and android:configChanges but can't seem to find a combo that does what I want.
- I can lock it into either landscape or portrait but I don't want to dictate which it should be.
android:screenOrientation="locked"locks it to portrait even if the phone is in landscape when I launch the app. - I can get it to start in the current orientation and ignore 90° turns, but it will still do a reorientation if I turn it upside down / 180°.
It's hard to Google because I just find lots of different questions display orientation and rotation but not my specific one. If it makes any difference I'm using Kotlin and need to support from Android 9.
You need to set the orientation programmatically based on the current orientation, in the
onCreatemethod of yourActivity:The actual challenge is finding the current orientation. Android's orientation API only provides
ORIENTATION_LANDSCAPEandORIENTATION_PORTRAITvalues, regardless of the device rotation degree. For example, both 90° and 270° rotations from normal portrait orientation will count asORIENTATION_LANDSCAPE. So in case this covers your needs, you can use the following method:In case you need to keep the actual rotation, you should use
DisplayAPIs:Note that the above code is considering only mobile phones. The provided surface angle is relative to the device's "natural" orientation.
0on a tablet is landscape, whereas0on a phone is portrait.