Search code examples
javaandroidaccelerometeraxis

How to swtich Accelerometer Axis based on Device?


My android application uses the Y Axis accelerometer to control the character in the game.

This is working fine on most mobile phones, however on my Galaxy Tab 2 10.1, the axis seems to be reversed and I have to manualy change the input for it work.

I would like to write a simple conditional statement that would swap based on device type, however I am unsure what the best practice for this would be. As I am sure someone would have come accross this issue before I ask the question.

How can I determine if the device is a table or a mobile in order to change Accelerometer axis?


Solution

  • I found out the best way to do this is to check the screen size on the device and base a boolean conditional to device on which axis to use. This is tested and working great.

    // Check the screen layout to determine if the device is a tablet.

    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);
    }
    

    // Changle Accelx or Accely input based on the above (Im using OpenGLES 1.1)

    if (isTablet(glGame)) {
        world.update(deltaTime, game.getInput().getAccelX());
    }
    if (!isTablet(glGame)) {
        world.update(deltaTime, game.getInput().getAccelY());
    }