Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)

Tags:

android

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?

like image 228
Urboss Avatar asked Dec 29 '10 11:12

Urboss


People also ask

How do I find the orientation of my device?

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.

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

What is the default orientation in android?

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.


2 Answers

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;     } } 
like image 106
user1035292 Avatar answered Sep 18 '22 10:09

user1035292


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)

like image 23
Nanne Avatar answered Sep 18 '22 10:09

Nanne