127

I think this is implementable since screen rotation behaviour can go up to the application level.

itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
Sam
  • 4,521
  • 13
  • 46
  • 81

8 Answers8

262

Yes it is implementable!

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

ActivityInfo

http://developer.android.com/reference/android/content/pm/ActivityInfo.html

Refer the link:

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);

buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }

});

buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   }

});

http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Thanks. But is this function that needs to be run on a rooted device? It seems not working on my cell phone but on my rooted tablet. – Sam Aug 16 '13 at 08:21
  • No it's not like that.. Actually i tested the code in that link before posting.. It was working in my device.. – Hariharan Aug 16 '13 at 08:36
  • okay, yea, you are right. i just tested another device and it works well. well, my Samsung Galaxy Nexus doesnt work. Dont know why. – Sam Aug 16 '13 at 08:43
  • 6
    Thanks for the answer. But this locking the application in that mode. When User is rotating the device the screen is not rotating. – Eco4ndly Feb 09 '18 at 05:24
  • 2
    If I set the orientation to landscape programatically as this answer, the user is not able anymore to change the orientation by rotating the device. Any way to handle this problem? I want to do what was taught in this answer but then when the user rotates the device it will keep changing the orientation. – Soon Santos Jul 30 '18 at 20:35
  • @SoonSantos I achieved something like that by starting an async tasked, delayed by a couple of seconds. Run myActivity.setRequestedOrientation(originalOrientation) in it. Of course this is ugly and comes with its own set of problems. – Heinzlmaen Mar 03 '20 at 09:47
  • That last link is wrong, take this: http://android-er.blogspot.com/2011/08/set-screen-orientation-programmatically.html – Janos Vinceller Jan 06 '21 at 14:58
45

Yes, you can set the screen orientation programatically anytime you want using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

for landscape and portrait mode respectively. The setRequestedOrientation() method is available for the Activity class, so it can be used inside your Activity.

And this is how you can get the current screen orientation and set it adequatly depending on its current state:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
final int orientation = display.getOrientation(); 
 // OR: orientation = getRequestedOrientation(); // inside an Activity

// set the screen orientation on button click
Button btn = (Button) findViewById(R.id.yourbutton);
btn.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {

              switch(orientation) {
                   case Configuration.ORIENTATION_PORTRAIT:
                       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                       break;
                   case Configuration.ORIENTATION_LANDSCAPE:
                       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                       break;                   
               }
          }
   });

Taken from here: http://techblogon.com/android-screen-orientation-change-rotation-example/

EDIT

Also, you can get the screen orientation using the Configuration:

Activity.getResources().getConfiguration().orientation
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • 3
    I'd suggest using `getRequestedOrientation()` to get the current screen orientation: http://stackoverflow.com/a/21909327/1037294 – a.ch. Feb 20 '14 at 13:39
  • `getRequestedOrientation()` gives you `UNSPECIFIED` when you start the app. So with the above listener it won't change screen and if you add `UNSPECIFIED` to the `switch` it should put the screen in one of **Portrait / Landscape** position first. – mortalis Sep 25 '18 at 08:13
25

Wherever possible, please don't use SCREEN_ORIENTATION_LANDSCAPE or SCREEN_ORIENTATION_PORTRAIT. Instead use:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

These allow the user to orient the device to either landscape orientation, or either portrait orientation, respectively. If you've ever had to play a game with a charging cable being driven into your stomach, then you know exactly why having both orientations available is important to the user.

Note: For phones, at least several that I've checked, it only allows the "right side up" portrait mode, however, SENSOR_PORTRAIT works properly on tablets.

Note: this feature was introduced in API Level 9, so if you must support 8 or lower (not likely at this point), then instead use:

setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
                        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
                        ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
                        ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
Paul
  • 331
  • 3
  • 5
21

Use this to set the orientation of the screen:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

or

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

and don't forget to add this to your manifest:

android:configChanges = "orientation"
Sathya
  • 678
  • 4
  • 12
  • 2
    You need both `"orientation|screenSize"` , Look here: https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange – Benny Mar 12 '17 at 17:28
13

A working code:

private void changeScreenOrientation() {
    int orientation = yourActivityName.this.getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        showMediaDescription();
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        hideMediaDescription();
    }
    if (Settings.System.getInt(getContentResolver(),
            Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
            }
        }, 4000);
    }
}

call this method in your button click

Liya
  • 568
  • 7
  • 28
1

Android-Kotlin:

to make rotate X:

binding.btnRotateHorizontal.setOnClickListener {
     requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}

to make rotate Y:

binding.btnRotateVertical.setOnClickListener {
     requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

Yes ، Hariharan answer works fine . but you should add below line to AndroidManifest.xml in activity tag :

android:screenOrientation="fullSensor"
android:configChanges="orientation|screenSize"

if not add above line , Hariharan answer not work .

Thanks Benny !

MH-Rouhani
  • 81
  • 1
  • 7
0

mine worked with:

setLandscapeIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setLandscapeIcon.setVisibility(View.GONE);
            setPortraitIcon.setVisibility(View.VISIBLE);
        }
    });

setPortraitIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                setLandscapeIcon.setVisibility(View.VISIBLE);
                setPortraitIcon.setVisibility(View.GONE);
            }
        });

In Manifest

android:configChanges = "orientation|screenSize"
android:screenOrientation="portrait"

NOTE: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); was not working on my device

Ashwini
  • 653
  • 6
  • 7