Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to toggle orientation lock in android?
    primarykey
    data
    text
    <p>I want to create checkbox in my preference Activity that allows user to toggle orientation change. </p> <p>In similar questions people write only about complete orientation lock (by overriding onConfigurationChanged method or adding configChanges in AndroidManifest.xml) or orientation enforcing ( by setRequestedOrientation ). </p> <p>Is there a way to toggle orientation lock? </p> <hr> <p>EDIT: I've created a method that sets preferred orientation to one of three states: landscape, portrait and sensor. This method is used in conjunction with retrieving orientation <code>getResources().getConfiguration().orientation)</code> and saving retrieved orientation into preferences. Then in activity that needs to lock orientation I fire this method with preferred orientation from preferences. </p> <pre><code>private static void setActivityOrientation(Activity activity, int preferenceOrientation) { if (preferenceOrientation == Configuration.ORIENTATION_LANDSCAPE) { if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ // You need to check if your desired orientation isn't already set because setting orientation restarts your Activity which takes long activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else if (preferenceOrientation == Configuration.ORIENTATION_PORTRAIT) { if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } else { if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload