Search code examples
androidandroid-orientationdevice-orientation

How can i detect orientation changes in portrait mode in android?


I need to show a camera preview in my app so that the photos can be taken by pushing a button. So I put a pretty "standard" camera preview class. The problem is I have to put it in landscape mode so that the preview can stretch on the entire screen. If in portrait mode, it will remain only on the center of the screen, on a small part of it. If this can be solved easier, I am open to suggestions.

But my question is. If locked in landscape mode, how can i detect screen rotation so that i can rotate text of the button accordingly ? I tried

@Override
public void onSensorChanged(SensorEvent event)
{
    Log.d("onSensorChanged");
    Log.d("event:" + event);

}

but I get no result.


Solution

  • Use OrientationEventListener:

    orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
        public void onOrientationChanged(int orientation) {
            //do something
        }
    };