user3093402
user3093402

Reputation: 1469

lock android activity in portrait mode without doing it for each activity

So I know that I can do android:screenOrientation="portrait" for each activity in my manifest and that will lock the activity to portrait mode. But I am wondering if there is a place in the manifest where I can enter this once and have it apply to the entire application.

Upvotes: 2

Views: 3000

Answers (3)

John
John

Reputation: 8946

Create a custom activity

public class CustomActivity extends Activity{

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }else{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

}

Then for all activities you create , for example ActicityA,ActivityB extend it from CustomActivity .

public class ActicityA extends CustomActivity {}

public class ActivityB extends CustomActivity {} 

Upvotes: 4

Swayam
Swayam

Reputation: 16354

Create a custom Activity in which you define the orientation and fix it to your desired position.

Inherit all the Activities in your application from your CustomActivity which has been defined with a fixed orientation.

EDIT : This answer provides yet another way of doing the same thing, but I like to believe that mine is more elegant because you wouldn't need to call the function for each and every Activity.

Upvotes: 3

IBunny
IBunny

Reputation: 319

In you manifest file you can give like this

   <activity
            android:name="com.androidcalapp.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">

As far as I know we have to repeat it for every activity in manifest. Or else you can handle it at runtime, by overriding onConfigurationChanged()method in Activity.

@Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

Upvotes: 2

Related Questions