I am implementing a way where the user can disable screen rotation from the app settings. If the box is checked then any Activity
can be automatically rotated and follow the phone rotation settings. If not checked then the autorotation is disabled.
I know how to do it per Activity
like this
if(!GlobalVar.sharedPreferences_static.isAutoRotate()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
Is there a way I can do this once for all the activities (the whole app) instead of doing it for every Activity
? Thank you.
Android SettingsStart by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”
In the menu, make sure Rotation Control and Per App Settings are enabled. By default, they should be. Tap Per App rotation settings, choose the apps whose settings you wish to alter, then change their settings.
If I understood your problem correctly, here is what you can do: create an empty Activity
(without setting its content) like this:
public class EmptyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your screen orientation logic here
}
}
and then you make all other Activities
extend the EmptyActivity
. So, you just need to implement your screen orientation logic once.
The same thing can be done in the manifest with:
android:screenOrientation="landscape"
but that attribute doesn't work if (only) applied to the application tag. You'd still have to put it on every activity, but at least it's syntactically easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With