windowsoftinputmode android screen-orientation

windowsoftinputmode - ¿Cómo obtengo la orientación CURRENT(ActivityInfo.SCREEN_ORIENTATION_*) de un dispositivo Android?



screen orientation ionic (8)

Me gustaría conocer la orientación detallada de un dispositivo, preferiblemente uno de SCREEN_ORIENTATION_LANDSCAPE , SCREEN_ORIENTATION_PORTRAIT , SCREEN_ORIENTATION_REVERSE_LANDSCAPE , SCREEN_ORIENTATION_REVERSE_PORTRAIT de ActivityInfo o su equivalente.

Algunas de las respuestas aquí en StackOverflow incluidas

getWindowManager().getDefaultDisplay().getRotation()

pero esto realmente no me dice si el dispositivo está en modo retrato o paisaje, solo cómo se gira con referencia a su posición natural, que a su vez puede ser paisaje o retrato en primer lugar.

getResources().getConfiguration().orientation

devuelve uno de los siguientes tres: ORIENTATION_LANDSCAPE , ORIENTATION_PORTRAIT , ORIENTATION_SQUARE , que en realidad no me dice en qué dirección está el teléfono (si está boca abajo o en qué lado está girado).

Sé que podría usar este último en combinación con DisplayMetrics para conocer la orientación natural del dispositivo, pero ¿realmente no hay una mejor manera?


¿Esto resuelve tu problema?

public static int getscrOrientation(Activity act) { Display getOrient = act.getWindowManager() .getDefaultDisplay(); int orientation = getOrient.getOrientation(); // Sometimes you may get undefined orientation Value is 0 // simple logic solves the problem compare the screen // X,Y Co-ordinates and determine the Orientation in such cases if (orientation == Configuration.ORIENTATION_UNDEFINED) { Configuration config = act.getResources().getConfiguration(); orientation = config.orientation; if (orientation == Configuration.ORIENTATION_UNDEFINED) { // if height and widht of screen are equal then // it is square orientation if (getOrient.getWidth() == getOrient.getHeight()) { orientation = Configuration.ORIENTATION_SQUARE; } else { // if widht is less than height than it is portrait if (getOrient.getWidth() < getOrient.getHeight()) { orientation = Configuration.ORIENTATION_PORTRAIT; } else { // if it is not any of the above it will defineitly // be landscape orientation = Configuration.ORIENTATION_LANDSCAPE; } } } } return orientation; // return value 1 is portrait and 2 is Landscape // Mode }


Creo que su problema es que puede detectar paisaje y retrato pero no invertir el paisaje e invertir el protrait, ya que no son compatibles con versiones anteriores. Para detectar lo que puede hacer es que puede usar tanto mantenimiento como rotación. Te estoy dando una idea de que puede ser útil para ti.

intente esto, creo que puede resolver su problema.

int orientation = getResources().getConfiguration().orientation; int rotation = getWindowManager().getDefaultDisplay().getRotation(); int actual_orientation = -1; if (orientation == Configuration.ORIENTATION_LANDSCAPE && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)){ orientation = Configuration.ORIENTATION_LANDSCAPE; } else if (orientation == Configuration.ORIENTATION_PORTRAIT && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)) { orientation = Configuration.ORIENTATION_PORTRAIT; } else if (orientation == Configuration.ORIENTATION_LANDSCAPE && (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)){ orientation = //any constant for reverse landscape orientation; } else { if (orientation == Configuration.ORIENTATION_PORTRAIT && (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)){ orientation = //any constant for reverse portrait orientation; } }


Podrías hacerlo de una manera muy simple: obtener la pantalla widht y height . el ancho de la pantalla siempre será mayor cuando el dispositivo esté en orientación horizontal.

Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Toast.makeText(getApplicationContext(), "" + width + "," + height, Toast.LENGTH_SHORT).show(); if (width > height) { Toast.makeText(getApplicationContext(), "LandScape", Toast.LENGTH_SHORT).show(); }


Terminé usando la respuesta anterior de Zoltán, que funciona muy bien, excepto cuando lo probé en una tableta (una Samsung P6210 Galaxy Tab 7.0 Plus). En modo retrato, devolvió SCREEN_ORIENTATION_REVERSE_PORTRAIT. Entonces, en la declaración else (si la orientación natural es el paisaje) cambié las cajas por ROTATION_90 y ROTATION_270, y todo parece funcionar bien. (No tengo suficiente reputación para publicar esto como un comentario a la respuesta de Zoltán).


Terminé usando la siguiente solución:

private int getScreenOrientation() { int rotation = getWindowManager().getDefaultDisplay().getRotation(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; int orientation; // if the device''s natural orientation is portrait: if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) { switch(rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; case Surface.ROTATION_270: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; default: Log.e(TAG, "Unknown screen orientation. Defaulting to " + "portrait."); orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; } } // if the device''s natural orientation is landscape or if the device // is square: else { switch(rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; case Surface.ROTATION_270: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: Log.e(TAG, "Unknown screen orientation. Defaulting to " + "landscape."); orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; } } return orientation; }

NOTA : Algunos usuarios (Geltrude y holtaf en los comentarios a continuación) señalaron que esta solución no funcionará en todos los dispositivos ya que la dirección de rotación de la orientación natural no está estandarizada.


Un enfoque simple sería usar

getResources().getConfiguration().orientation

1 es para Potrait y 2 para Landscape.


getResources().getConfiguration().orientation es la forma estándar de conocer la orientación actual que se está utilizando. Sin embargo, si no satisface sus necesidades, entonces tal vez pueda usar sensores para calcularlo en términos de ángulo. Lee this y this


public static int getScreenOrientation(Activity activity) { int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = activity.getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_PORTRAIT) { if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) { return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } else { return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } } if (orientation == Configuration.ORIENTATION_LANDSCAPE) { if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) { return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } } return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; }