Mian Muhammad Umar
Mian Muhammad Umar

Reputation: 45

make orientation of an activity both landscape and portrait

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

Answers (2)

amatellanes
amatellanes

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

Waza_Be
Waza_Be

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

Related Questions