Set screen orientation for the entire application

I am implementing a method by which a user can turn off screen rotation from application settings. If the checkbox is checked, then any Activity can automatically rotate and follow the phone rotation settings. If unchecked, autorotation is disabled.

I know how to do this for an Activity like this

 if(!GlobalVar.sharedPreferences_static.isAutoRotate()){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }else{ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); } 

Is there a way to do this once for all actions (the entire application) instead of doing this for each Activity ? Thanks.

+6
source share
3 answers

If I understand your problem correctly, here is what you can do: create an empty Activity (without setting its contents) as follows:

 public class EmptyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Your screen orientation logic here } } 

and then you do all the other Activities continue with EmptyActivity . Thus, you just need to implement the screen orientation logic once.

+2
source

The same can be done in the manifest with:

 android:screenOrientation="landscape" 

but this attribute does not work if (only) is applied to the application tag. You still have to impose this on every action, but at least syntactically easier.

0
source

You just add this line to your activity like this

  <activity android:name="Package name" android:label="App anem" android:screenOrientation="landscape" //Orientation > </activity> 

Or you can also program like this at the beginning of the action ...

 if (Config.sDeviceType.equalsIgnoreCase("MO")) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } 
0
source

All Articles