0

Let's say that I have a game where when I rotate my phone to the left, my players walks left etc. Everything is set on SensorEvent on 'y' position. Here comes an issue. The whole app is running on Landscape mode (which is set in AndroidManifest.xml) and coded by:

  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

So. I have a method that checks if the device is a tablet or a smartphone and it's working, But - on half of the tablets the 'view' is vertical - I mean they act like phones.

On Samsung Galaxy S4 (mobile phone) I check the y and -y position (cause I rotate the phone to hold in 2 hands) - it works. On Samsung Galaxy Tab I keep the tablet in 2 hands normally, I use '-x' position (no idea why it's reversed - should be x) - it works. ISSUE: Tablets like Prestigio. They are all VERTICAL - like smartphones. When I run my 'isTablet' it checks the size of the screen and it says that it's a tablet, but the x, y, z system of the sensor is acting like on a phone (I have to use 'y').

Does anyone know how to handle all the devices?

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
  • Could you use the reported screen size and check whether x or y is bigger in that to determine which way the device was working? If x/width is bigger it is in landscape (classic tablet) and if y/height is bigger then it is in portrait (classic phone). Compare that information with `isTablit` and you could probably know all the scenarios? – Neil Townsend Sep 23 '15 at 07:41
  • Galaxy Tab: WIDTH 2560 HEIGHT 1600 Prestigio Tablet WIDTH 2048 HEIGHT 1440 Galaxy S4 - Phone : WIDTH 1920 HEIGHT 1080 On every device the width is bigger. Tablets like Prestigio are just vertical from the start. When you launch them it shows it's company name vertically. On Samsung Tab it's horizontally. –  Sep 23 '15 at 07:42
  • is that the spec or is that what the device returns (see http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels) – Neil Townsend Sep 23 '15 at 07:46
  • This was from metrics = new DisplayMetrics(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getMetrics(metrics); Should I check from this method that you posted? –  Sep 23 '15 at 07:56
  • The methods are essentially the same, so no point in trying the other one. Without wishing to pry into your project, if it using a custom full screen view, you can get the actual size (and shape) of the screen from `onScreenResize()` in the custom view. Perhaps you could cvatch the information there? – Neil Townsend Sep 23 '15 at 08:09
  • I can't attach this method to anything (I have MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener). Maybe I just should check these values in the first class in OnCreate? –  Sep 23 '15 at 08:22
  • if you add a method `protected void onSizeChanged(int new_w, int new_h, int old_w, int old_h)` to your SurfaceView, it will be called when the associated view changes size, as well as at start up. – Neil Townsend Sep 23 '15 at 09:07
  • Okay, used this method on every device. Values are totally the same :( –  Sep 23 '15 at 09:42
  • So on the Prestigio, when held like a phone, it reports a width greater than height? – Neil Townsend Sep 23 '15 at 09:44
  • Yes. What is totally wrong. http://www.gsmarena.com/prestigio_multipad_4_quantum_9_7-5655.php Spec says that the width is lower O.o –  Sep 23 '15 at 09:49
  • So that means the `onSizeChanged` is reporting the information the wrong way round? So if you tried to draw, you are in trouble! – Neil Townsend Sep 23 '15 at 10:40
  • The best thing is that all of the graphics are drawn very well, even on Prestigio. While drawing on canvas, I have 'borders' on 0, 0, screen_width, screen_height - to support all devices. My only idea is that If config is overwritten by RequestFeature(LANDSCAPE) and in Manifest it just always reverses the dimensions to width>height :( Dunno, I will try to ask in Android :( –  Sep 23 '15 at 11:02
  • So what are the value for screen_width and screen_hieght then? Do they have the same problem? – Neil Townsend Sep 23 '15 at 13:22
  • No, when I rotate, the phone Height is smaller - everything just works fine. When I want to draw something in the middle I just draw on (width/2,height/2). It works even on Prestigio... –  Sep 23 '15 at 14:47

1 Answers1

0

If you have both isTablet() and know what the actual screen looks like to the user, which your final comment leads my to believe, then the following code should set a flag, useY which describes whether to use Y or not (in which case, use X) ...

// The screen as the user seles it (ie changes with rotation)
int screen_width; (the actual screen width of the phone)
int screen_height; (ditto, height)

// A flag to determine whether to use y or x for the motion detection.
boolean useY = true;

// The logic
if (screen_width > screen_height) { // Landscape (classic tablet)
    if (isTablet()) {
        // Normal: the screen is landscape and the device is a tablet. Use X
        useY = false;
    } else {
        // The device is a phone and the screen is landscape. Based on your
        // comments above, this is also normal
        useY = true;
    }
}
else { // Portrait (classic phone)
    if (isTablet()) {
        // This is the odd case you describe
        useY = false;
    } else {
        // A phone in portrait - should never happen.
    }
}
Neil Townsend
  • 6,024
  • 5
  • 35
  • 52