Reputation: 5295
I have a video player where user can change orientation manually also by rotating the device and by pressing a button click. When i am clicking the button, orientation changes to landscape but now the orientation gets locked and user cannot move back to portrait just by rotating the device I have tried this :-
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and this :-
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
but none of it seems to work. So, how can i keep the orientation unlocked at all times plus user can switch orientation with a button click keeping the orientation unlocked ?
Upvotes: 7
Views: 1984
Reputation: 53
I also had to change one activity's orientation to 180 when opening it and then let the user rotate back to portrait and lock it.
I just added these lines inside onCreate:
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
The activity is locked in portrait mode in manifest.
<activity
android:name=".ui.GalleryActivity"
android:launchMode="singleTask"
android:screenOrientation="sensorPortrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Upvotes: 0
Reputation: 798
After I changing the orientation using a button, I changed the orientation to unspecified within a handler to assure that the orientation has been changed. Here is my code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
new Handler().postDelayed(new Runnable() {
public void run() {
// make screen orientation unspecified (sensor change it
//according to user action)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}, 3000);
Upvotes: 2
Reputation: 86
Add android:screenOrientation="portrait"> in the application section.
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".App"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Upvotes: 0
Reputation: 2263
Try this,after changing your desire condition
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Upvotes: 2