Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable and disable auto rotate programmatically?

Tags:

There are a lot of cool widgets out there that will enable and disable auto rotate on your phone. Disabling it turns it off across all apps on the phone.

Any ideas how they are accomplishing it?

like image 892
Cameron McBride Avatar asked Feb 05 '11 16:02

Cameron McBride


People also ask

How do I turn off my auto rotation?

Short guide: Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.

How do I lock orientation in android programmatically?

setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); Called on an activity, will lock it to landscape.


1 Answers

This should do the trick for you:

    import android.provider.Settings;     public static void setAutoOrientationEnabled(Context context, boolean enabled)     {           Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);     } 

Add permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

You can find the documentation here

like image 155
Lior Avatar answered Oct 22 '22 15:10

Lior