Control default auto rotate screen in my application
Asked Answered
H

4

5

I have a toggle button in my application. I want to change or control default setting, Auto rotate screen(Settings>Display>Auto rotate screen) programmatically. Does anybody know how to do this?

Hoyos answered 15/3, 2012 at 10:58 Comment(0)
D
10

Have you tried this in your Activity?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//This is the default value
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

After that you can use this to disable the auto orientation:

public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
  Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

Documentation

Dogmatize answered 15/3, 2012 at 11:59 Comment(2)
I don't want to change the orientation of a activity alone, but want to change the Settings. I want to enable and disable Auto rotation screen of Android settings.Hoyos
After exiting from my application, the device should not be able to auto rotate the screen.Hoyos
S
7

you can use this:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.USER ROTATION,user_rotation);

for rotation policy

user_rotation 0 -> ROTATION_0
user_rotation 1 -> ROTATION_90
user_rotation 2 -> ROTATION_180
user_rotation 3 -> ROTATION_270

see http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION for more.

Also menifiest.xml setting

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Starchy answered 15/3, 2012 at 12:47 Comment(0)
V
3

You set default rotation settings in your manifest file e.g:

<activity android:name=".MainTabActivity" android:screenOrientation="portrait">
</activity>

To change orientation programatically you have to call Activity.setRequestedOrientation()

Veiled answered 15/3, 2012 at 12:31 Comment(1)
After exit of my application, I want to set device's Auto rotation setting(Settings>Display>Auto rotation screen) false through my code. How can I achieve this?Hoyos
P
0
a1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.USER_ROTATION,0);
    }
});
a2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.USER_ROTATION,90);
    }
});
Pacifically answered 17/2, 2014 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.