OrientationEventListener (Tablet vs Mobile) 90 degree difference

Im using an OrientationEventListener to determine the orientation, but I have a problem in that the tablets are landscape and the phones are portraits by default. This means that OrientationEventListener returns a value of 90 for a portrait on tablets, but 0 for a portrait on mobile phones.

The activity I use has a camera, so I can’t change the orientation, so I use the Orientation value to reposition several elements on the screen as needed.

Is it possible to determine if the device is a tablet so that I can adjust the value accordingly. How can I determine the value of isTablet?

            if(isTablet)
            {
                orientation += -90;
                if(orientation < 0) //Check if we have gone too far back, keep the result between 0-360
                {
                    orientation += 360;
                }   
            }
+3
source share
4

, , , , .

.

android.os.Build.DEVICE, android.os.Build.MODEL android.os.Build.PRODUCT, , this, , .

, , . ( ).

- , , :

public boolean isTablet(Context context) {
    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    return (xlarge || large);
}

XLARGE ( , ).

, !

+2

. , : ( ) Android (.. , , Motorola Charm Flipout). getDeviceDefaultOrientation() isTablet :

isTablet = (getDeviceDefaultOrientation() == Configuration.ORIENTATION_LANDSCAPE)

, getDeviceDefaultOrientation() ( ) . , , , , . , Activity.setRequestedOrientation() , getDeviceDefaultOrientation().

+1

, , , "" . , , , :

switch (yourContext.getResources().getConfiguration().orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        //xxx
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        //xxx
        break;
}

"onCreate" =)

0

, , ( :). , ( ).

== , ( ,... , ). , / ( , 2 ).

int getScreenOrientation(Activity ctx)  {
  Point pt = new Point(0,0);
  try {
    Display dsp =  ctx.getWindowManager().getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
      dsp.getSize(pt);
    } else {
      pt.x = dsp.getWidth();   
      pt.y = dsp.getHeight();
    }
  return  (pt.x <= pt.y) ?  
    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  }
  catch (Exception e) {}
  return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}

This only solves the orientation problem, it does not detect if it is a tablet. In fact, most of my 7inch tabs have no problem with orientation mismatch. I found it only on my NEXUS 10 and on the TV android.

"Do not code like me, and everything will be fine with me."

Sean

0
source

All Articles