Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); if (display.getOrientation() == Surface.ROTATION_0) { // landscape oriented devices } else { // portrait oriented device }
private int _getScreenOrientation(){ Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); return display.getOrientation(); }
public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int orientation = display.getOrientation(); switch(orientation) { case Configuration.ORIENTATION_PORTRAIT: if(!oAllow) { setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } break; case Configuration.ORIENTATION_LANDSCAPE: if(!oAllow) { setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } break; } }
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; } } });
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); Display mDisplay = mWindowManager.getDefaultDisplay(); Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation()); }
if (display.getOrientation() == Surface.ROTATION_0) { // landscape oriented devices if (orientation >= 315 || orientation < 45) { if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
public DisplayAssert hasOrientation(@SurfaceRotation int orientation) { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> but was <%s>", orientationToString(orientation), orientationToString(actualOrientation)) // .isEqualTo(orientation); return this; }
public DisplayAssert isPortrait() { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> or <%s>, but was <%s>", orientationToString(ROTATION_0), orientationToString(ROTATION_180), orientationToString(actualOrientation)) // .isIn(ROTATION_0, ROTATION_180); return this; }
public DisplayAssert isLandscape() { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> or <%s>, but was <%s>", orientationToString(ROTATION_270), orientationToString(ROTATION_90), orientationToString(actualOrientation)) // .isIn(ROTATION_270, ROTATION_90); return this; }
/** * The {@link android.view.Display#getOrientation()} method is deprecated, but for * testing purposes, return the value gotten from {@link android.view.Display#getRotation()} */ @Test public void deprecatedGetOrientation_returnsGetRotation() { int testValue = 33; shadow.setRotation(testValue); assertEquals(testValue, display.getOrientation()); } }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void changeDisplay_shouldAllowPartialChanges() throws Exception { List<String> events = new ArrayList<>(); instance.registerDisplayListener(new MyDisplayListener(events), null); int displayId = ShadowDisplayManager.addDisplay("w100dp-h200dp"); ShadowDisplayManager.changeDisplay(displayId, "+h201dp-land"); Display display = getGlobal().getRealDisplay(displayId); assertThat(display.getWidth()).isEqualTo(201); assertThat(display.getHeight()).isEqualTo(100); assertThat(display.getOrientation()).isEqualTo(Surface.ROTATION_90); assertThat(events).containsExactly( "Added " + displayId, "Changed " + displayId); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void changeAndRemoveDisplay_shouldNotifyListeners() throws Exception { List<String> events = new ArrayList<>(); instance.registerDisplayListener(new MyDisplayListener(events), null); int displayId = ShadowDisplayManager.addDisplay("w100dp-h200dp"); ShadowDisplayManager.changeDisplay(displayId, "w300dp-h400dp"); Display display = getGlobal().getRealDisplay(displayId); assertThat(display.getWidth()).isEqualTo(300); assertThat(display.getHeight()).isEqualTo(400); assertThat(display.getOrientation()).isEqualTo(Surface.ROTATION_0); ShadowDisplayManager.removeDisplay(displayId); assertThat(events).containsExactly( "Added " + displayId, "Changed " + displayId, "Removed " + displayId); }
System.out.println(rotation+""); if (display.getOrientation() != Surface.ROTATION_0) { // landscape oriented devices System.out.println("LANDSCAPE"); if (orientation >= 315 || orientation < 45) {
Display myDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int rotation = myDisplay.getOrientation(); switch (rotation) { case Surface.ROTATION_0: { doSomething(); break; } case Surface.ROTATION_90: { doSomething(); break; } case Surface.ROTATION_270: { doSomething(); break; } case Surface.ROTATION_180: { doSomething(); break; } }
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int orientation = display.getOrientation(); if(orientation == Configuration.ORIENTATION_PORTRAIT){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } if(orientation == Configuration.ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)) .getDefaultDisplay(); int orientation = display.getOrientation(); // use this to see on your console System.out.println("Screen Size: " + orientation); // use this to see on logcat Log.v(TAG, "Screen Size:" + orientation);
/* First, get the Display from the WindowManager */ Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); /* Now we can retrieve all display-related infos */ int width = display.getWidth(); int height = display.getHeight(); int orientation = display.getOrientation();
public DisplayAssert hasOrientation(@SurfaceRotation int orientation) { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> but was <%s>", orientationToString(orientation), orientationToString(actualOrientation)) // .isEqualTo(orientation); return this; }
public DisplayAssert isLandscape() { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> or <%s>, but was <%s>", orientationToString(ROTATION_270), orientationToString(ROTATION_90), orientationToString(actualOrientation)) // .isIn(ROTATION_270, ROTATION_90); return this; }
public DisplayAssert isPortrait() { isNotNull(); int actualOrientation = actual.getOrientation(); //noinspection ResourceType assertThat(actualOrientation) // .overridingErrorMessage("Expected orientation <%s> or <%s>, but was <%s>", orientationToString(ROTATION_0), orientationToString(ROTATION_180), orientationToString(actualOrientation)) // .isIn(ROTATION_0, ROTATION_180); return this; }