Question

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.

Was it helpful?

Solution

Use OrientationEventListener:

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

OTHER TIPS

use this simple method...

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
     // your code
}

OR

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
    // your code
}

First of all you should know that what happen whenever screen mode changed.

When screen mode changed its view created again it means if we can detect what screen mode is we can change our layout file or do other things in onCreate().

How how you can detect which screen mode is.

if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
    //do something
}

Configuration class have different orientation Constant check it on developer site.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top