synic
synic

Reputation: 26668

Programmatically lock into portrait mode for certain operations

Is there a way to programmatically lock an app in portrait mode for certain operations, and then resume (and have the app rotate to landscape if the user is holding the device that way) after the operation is complete?

Upvotes: 48

Views: 28968

Answers (3)

Rehan Khan
Rehan Khan

Reputation: 1281

Just use this in OnCreate Method of Activity if you want to set Screen only in Portrait

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

If you want only LANDSCAPE so use this line

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

This one Line is Enough

Like this below

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

Upvotes: 0

OceanBlue
OceanBlue

Reputation: 9248

Try this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Do your operation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Upvotes: 99

Micah Hainline
Micah Hainline

Reputation: 14427

I'm reading into your question a bit, but if the problem you are facing is that the reload of the activity causes problems, you can add in the manifest a line to handle the orientation changes yourself. You'll probably need to do this anyway if you want to do something special for the orientation changes, but perhaps just adding the lines will take care of your problems for you:

<activity android:name=".MyActivity"  
    android:configChanges="keyboard|keyboardHidden|orientation" />

Upvotes: 4

Related Questions