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?
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?
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
Always use user specified screen orientation, this will apply whatever orientation user has choose and will not rotate screen if its disabled.
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
This is my impementation for this problem. I had to implement a button which had the same function that the lockbutton from the settings menu.
you can use the setRotationScreenFromSettings to solve your issue
public static boolean getRotationScreenFromSettingsIsEnabled(Context context)
{
int result = 0;
try
{
result = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
}
catch (Settings.SettingNotFoundException e)
{
e.printStackTrace();
}
return result == 1;
}
public static void setRotationScreenFromSettings(Context context, boolean enabled)
{
try
{
if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1)
{
Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
}
else
{
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
catch (Settings.SettingNotFoundException e)
{
e.printStackTrace();
}
}
private void regirsterLockScreenOrientationChangedListner()
{
ContentObserver rotationObserver = new ContentObserver(new Handler())
{
@Override
public void onChange(boolean selfChange)
{
refreshLockScreenOrientationBtn();
}
};
context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
true, rotationObserver);
}`
Add permission to the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
You have to fix it using following code inside onCreate() Function. Working...
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);