Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android | How to keep orientation unlocked even after changing orientation programmatically?

Tags:

android

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 ?

like image 458
Rahul Gupta Avatar asked Sep 06 '16 09:09

Rahul Gupta


People also ask

How do you prevent the data from reloading and resetting when the screen is rotated?

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.


2 Answers

Try this,after changing your desire condition

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
like image 61
Htoo Aung Hlaing Avatar answered Oct 20 '22 23:10

Htoo Aung Hlaing


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);
like image 28
Matin Ashtiani Avatar answered Oct 20 '22 22:10

Matin Ashtiani