user3195199
user3195199

Reputation: 91

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.

Upvotes: 0

Views: 1468

Answers (3)

ParikshitSinghTomar
ParikshitSinghTomar

Reputation: 417

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.

Upvotes: 0

Umang Kothari
Umang Kothari

Reputation: 3694

use this simple method...

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

OR

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

Upvotes: 0

Green goblin
Green goblin

Reputation: 9996

Use OrientationEventListener:

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

Upvotes: 1

Related Questions