2

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.

user3093402
  • 1,419
  • 4
  • 20
  • 28

3 Answers3

4

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 {} 
John
  • 8,846
  • 8
  • 50
  • 85
  • 1
    This will definitely do it. But I was rather hoping that android had a way of doing this in the manifest. It won't always be convenient to extend that custom activity, as there are multiple types of activities, but all the same this idea would reduce the work load. +1 – user3093402 Jan 11 '14 at 18:41
  • 2
    Why would you have an if else statement that executes the same code? – dot_zero Feb 29 '16 at 20:59
3

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.

Community
  • 1
  • 1
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • The link you provide is in fact more versatile than your own answer. As I told @John, it's not always convenient to extend CustomActivity. +1 – user3093402 Jan 11 '14 at 18:44
  • Thank you for the *up*. And well, I read your comment for @John's answer. I have yo say that the thought of having different types of activities never came to my mind. Thank you very much for pointing it out. :) – Swayam Jan 13 '14 at 03:23
2

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);
        }
IBunny
  • 309
  • 9
  • 20
  • 1
    so you just repeated what I said I already know. And somehow someone gave you an up vote for it. – user3093402 Jan 09 '14 at 20:01
  • @user3093402 That is what I said, we have to repeat it in manifest or else can handle at run time as I updated the answer above. – IBunny Jan 10 '14 at 05:10
  • @IBunny : The OP is talking about doing it once in the whole application. For example, he might have 30 Activities. So, it is a little inefficient to copy and paste the same code 30 times. :) – Swayam Jan 10 '14 at 06:22
  • @Swayam Thank you for the clarification. But I got what he is asking. That is why I edited my previous answer and I tested it too. :) – IBunny Jan 12 '14 at 22:35