javaandroidscreen-rotationgoogle-mlkit

Set Rotation based on device sensor overriding rotation lock


I have been trying to use the ML Kit Vision Quickstart Sample App to develop a blink detection app. For this, I have been using the Face Detector module with the CameraX library. On installing the sample app on my Android phone, I found that rotating the device with Screen Rotation locked makes the app unable to detect faces, as expected. I want to know how I should modify the code to override the Screen Rotation lock on the device, so that the app automatically rotates its orientation to detect faces.


Solution

  • To override the screen lock you may add the next tag to your target activity in the manifest:

    android:screenOrientation="sensor"
    

    Although this will only allow to rotate the activity regardless the screen lock. You still need to handle the orientation changes for the camera code. What I mean is that your camera lens is a fixed device and it doesn't rotate, therefore you need to handle image rotation by code.