Programmatically lock into portrait mode for certain operations

Viewed 28278

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?

3 Answers

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

    }
Related