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 ?
You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape" ) to <activity> in your manifest. You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="orientation|screenSize" in the <activity> tag.
Try this,after changing your desire condition
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With