I have an activity showing preview from camera, so it need to be set as landscape only. At the bottom (regardless of device rotation) I want to show a text view. I am using OrientationEventListener which gives device's orientation from its natural position. I can implement a solution which works well on portrait default devices but to make it work also on landscape default devices I need to be aware of running on such a device. Thus the question is how to check it?
If the activity locks the display ( android:screenOrientation="portrait" ), this method will return the same value irrespective of how the user rotates the device. In that case you'd use the accelerometer or the gravity sensor to figure out orientation properly.
int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.
The default screen orientation for the Android-ready Nitrogen and Nitrogen53 is landscape. That's fine for most people, but say you want it in portrait. Or upside down, or whatever.
This method can help:--
public int getDeviceDefaultOrientation() { WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Configuration config = getResources().getConfiguration(); int rotation = windowManager.getDefaultDisplay().getRotation(); if ( ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE) || ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)) { return Configuration.ORIENTATION_LANDSCAPE; } else { return Configuration.ORIENTATION_PORTRAIT; } }
Well, you can find out what current orientation is the way @Urboss said. You cannot get the layout (landscape/portrait) that way ofcourse, so you'll have to get the screen width/heigth to check if the current (be it changed or not, but you've checked that ;) ) position is landscape or portrait:
DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); wPix = dm.widthPixels; hPix = dm.heightPixels;
(so if the rotation is 0 and you get landscape with above metrix, your device is default landscape. If the rotation is 90 degrees and you're portrait, the default is also landscape, and so on)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With