user6281889
user6281889

Reputation: 15

Change android screen orientation from code

I want my application to be able to get the current screen orientation and change it to landscape/portrait. Which things do I need to declare in the manifest file to deal with screen orientation ?

Upvotes: 1

Views: 1894

Answers (2)

Sarantis Tofas
Sarantis Tofas

Reputation: 5167

If you want to set the orientation to Landscape:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

If you want to set the orientation to Portrait:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Also add android:configChanges="orientation|screenSize" in your manifest to avoid reloading the activity on orientation change.

Upvotes: 1

Ashish
Ashish

Reputation: 183

For getting the orientation u can use:

getResources().getConfiguration().orientation

And for setting the orientation:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

Upvotes: 0

Related Questions