Change Screen Orientation programmatically using a Button Change Screen Orientation programmatically using a Button android android

Change Screen Orientation programmatically using a Button


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


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 clickButton 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


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