Reputation: 5025
I think each android device has an abitily to on/off auto-rotating function.
Usually you can find it in settings->display->auto-rotate on/off
. How can I read this setting state from my application? How can I access to this setting value? If you can share a code snipped i'd be very appreciate it.
Upvotes: 14
Views: 13810
Reputation: 2877
Use the following code:
if (android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Rotation.this, "Rotation OFF", Toast.LENGTH_SHORT).show();
}
Upvotes: 3
Reputation: 2401
Hope this code snippet helps you out:-
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
if (android.provider.Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
Toast.makeText(getApplicationContext(), "Rotation ON", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Rotation OFF", Toast.LENGTH_SHORT).show();
}
super.onCreate(savedInstanceState);
}
Upvotes: 32
Reputation: 1495
Try this:
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
Upvotes: 0