spectre_d2
spectre_d2

Reputation: 203

How does one detect orientation of an Android device?

To get orientation, I have tried the below codes. But I don't get the desired output.

int rotation = getWindowManager().getDefaultDisplay().getRotation();

and

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

I have tried this both of code and rotate the device every angle but always get rotation value 0.

Then I have tried this below code

int rotation = this.getResources().getConfiguration().orientation;

But now I am always getting rotation value 1. How I will get different value of rotation for Natural orientation, Landscape left, Upside down, Landscape right?

Upvotes: 2

Views: 292

Answers (2)

Neïl Rahmouni
Neïl Rahmouni

Reputation: 352

You can use getRequestedOrientation() and it will return one of these values.

Hope this helps !

Upvotes: 1

Kintan Patel
Kintan Patel

Reputation: 1268

Activity.getResources().getConfiguration().orientation

Upvotes: 0

Related Questions