Snake
Snake

Reputation: 14648

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.

Upvotes: 6

Views: 1210

Answers (3)

Onik
Onik

Reputation: 19959

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.

Upvotes: 2

scottt
scottt

Reputation: 8371

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.

Upvotes: 1

Naveen Kumar Kuppan
Naveen Kumar Kuppan

Reputation: 1442

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);
        }

Upvotes: 0

Related Questions