CL So
CL So

Reputation: 3759

How to get Android device orientation when launch the app

I tried this, not work, I always got 0 degree

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenOrientation = display.getRotation();

If use SensorManager, it is also not work, because it will not be triggered if I don't move the phone

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

    @Override
    public void onOrientationChanged(int orientation) {
    }
};

Update

Please try the complete demo

https://github.com/sochap/getorientationquestdemo

Since the display is not changed, use display to determine that is impossible

I am going to get device orientation, not display orientation

OrientationEventListener will work, but not at onCreate

Upvotes: 1

Views: 877

Answers (2)

Learning Always
Learning Always

Reputation: 1561

This may work for you. try this.

 Configuration config = getResources().getConfiguration();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {

                     // set Values for landscape

 } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {

                   // set Values for portrait
    }

Upvotes: 1

Mithun Sarker
Mithun Sarker

Reputation: 4023

Try

int currentOrientation = getResources().getConfiguration().orientation;

if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
     //do something
} else {
     //do something
} 

Upvotes: 0

Related Questions