Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set screen orientation for the whole app

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.

like image 385
Snake Avatar asked Mar 31 '14 05:03

Snake


People also ask

Can you change the orientation of an app?

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.”

How do I turn off auto rotate for certain apps?

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.


2 Answers

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.

like image 85
Onik Avatar answered Oct 16 '22 05:10

Onik


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.

like image 30
scottt Avatar answered Oct 16 '22 05:10

scottt