Reputation: 45
I want my activity to be landscape when device is horizontal and portrait when device is vertical. How can I do this?
Upvotes: 1
Views: 3450
Reputation: 3735
In your AndroidManifest.xml of your activity, when you define a new activity you must writeandroid:screenOrientation="unspecified"
:
<activity
android:screenOrientation="unspecified"
android:name="com.example.MainActivity"
android:label="@string/app_name" >
Or write nothing, because unspecified
is the default value.
If you have to change orientation programmatically, you must write this:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Upvotes: 4
Reputation: 39538
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
As the device has an accelerometer, you can easily switch between both
I hope this will help.
Upvotes: 2