Cambiar la orientación de la pantalla mediante programación mediante un Botón


Creo que esto es implementable ya que el comportamiento de rotación de la pantalla puede subir al nivel de la aplicación.

Author: itsaboutcode, 2013-08-16

5 answers

Sí es 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

Consulte la enlace:

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

 162
Author: Hariharan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-24 09:46:50

Sí, puede establecer la orientación de la pantalla programáticamente en cualquier momento que desee utilizando:

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

Para el modo horizontal y vertical respectivamente. El método setRequestedOrientation () está disponible para la clase de actividad, por lo que se puede usar dentro de su Actividad.

Y así es como puedes obtener la orientación actual de la pantalla y configurarla adecuadamente dependiendo de su estado actual:

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

Tomado de aquí: http://techblogon.com/android-screen-orientation-change-rotation-example /

EDITAR

Además, puede obtener la orientación de la pantalla usando el Configuration:

Activity.getResources().getConfiguration().orientation
 29
Author: Philipp Jahoda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-18 10:27:50

Siempre que sea posible, por favor no use SCREEN_ORIENTATION_LANDSCAPE o SCREEN_ORIENTATION_PORTRAIT. En su lugar use:

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

Estos permiten al usuario orientar el dispositivo a una orientación horizontal o vertical, respectivamente. Si alguna vez has tenido que jugar un juego con un cable de carga que se introduce en el estómago, entonces sabes exactamente por qué tener ambas orientaciones disponibles es importante para el usuario.

Nota: Para los teléfonos, al menos varios que he comprobado, se solo permite el modo retrato "lado derecho hacia arriba", sin embargo, SENSOR_PORTRAIT funciona correctamente en tabletas.

Nota: esta característica se introdujo en el nivel de API 9, por lo que si debe admitir 8 o inferior (no es probable en este punto), 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);
 16
Author: Paul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-10 17:58:34

Use esto para establecer la orientación de la pantalla:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

O

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Y no olvides añadir esto a tu manifiesto:

android:configChanges = "orientation"
 15
Author: Sathya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-16 09:20:58

Un código de trabajo:

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

Llame a este método en su botón haga clic en

 1
Author: Liya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-28 10:27:04