Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

You just add this line into your activity like this

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

Or you can set like this programmatically like this in the starting of the activity...

if (Config.sDeviceType.equalsIgnoreCase("MO")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top