Changing the screen orientation programmatically using the button

I think this is feasible since the screen rotation behavior can go to the application level.

+82
android uibutton screen-orientation android-button
Aug 16 '13 at 7:38 on
source share
5 answers

Yes it is possible!

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

+187
Aug 16 '13 at 7:43
source share

Yes, you can adjust the screen orientation programmatically anytime you want:

 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 here is how you can get the current screen orientation and adjust it accordingly depending on the 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

Alternatively, you can get the screen orientation using Configuration :

 Activity.getResources().getConfiguration().orientation 
+34
Aug 16 '13 at 7:52
source share

Use this to set the screen orientation:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

or

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

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

 android:configChanges = "orientation" 
+17
Aug 16 '13 at 9:20
source share

If possible, do not use SCREEN_ORIENTATION_LANDSCAPE or SCREEN_ORIENTATION_PORTRAIT. Use instead:

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

This allows the user to orient the device both in landscape orientation and portrait orientation. If you've ever had to play a game with a charging cable in your stomach, then you know exactly why the presence of both orientations is important for the user.

Note. For at least a few phones that I checked, it only allows portrait mode "right side", however, SENSOR_PORTRAIT works correctly on tablets.

Note. This feature was introduced at API level 9, so if you must support 8 or lower (unlikely at the moment), 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); 
+17
Aug 10 '15 at 17:58
source share

Work 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

+4
Mar 28 '18 at 10:27
source share



All Articles