Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Device Orientation is Landscape but Sensors Behaves Like the Orientation is Portrait

I have a problem with orientation in android. The device orientation is landscape which i have set in AndroidManifest.xml file.

android:screenOrientation="landscape"

Also I am holding the device in landscape, so everything (layouts, views, etc.) is in landscape mode as I wanted it to be. But only when I hold the device in portrait it gives me values that I need.

I modified some code from internet and it works for HTC 3d EVO, Nexus 7 and Samsung Galaxy S3 but not in Galaxy Tablet 10".

I have found this post. Android Orientation Sensor Different for Different Devices? The accepted answer suggests using getRotation() and remapCoordinateSystem() methods but not mentions about how.

This is how I am ended up.

        SensorManager.getRotationMatrix(Rmat, Imat, gData, mData);
        SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);
        // Orientation isn't as useful as a rotation matrix, but
        // we'll show it here anyway.
        SensorManager.getOrientation(R2, orientation);
        float incl = SensorManager.getInclination(Imat);

        FontTextView pitchText = (FontTextView)findViewById(R.id.pitch_text);

        if((int)(orientation[1]*90) <= -110 && !newQuestionIsActive) {
            newQuestionIsActive = true;
            pitchText.setText("Bring new question");
        }
        else if(Math.abs((int)(orientation[2]*90)) <= 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) {
            newQuestionIsActive = false;
            pitchText.setText("Not Correct!");
        }
        else if(Math.abs((int)(orientation[2]*90)) > 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) {
            newQuestionIsActive = false;
            pitchText.setText("Congratulations!");
        }

Now how am I supposed to make use of getRotation() in this code? And may be a brief explanation would be appreciated why those mentioned 3 devices work fine but not the Galaxy Tablet.

About this problem android documentation says;

Finally, if your application matches sensor data to the on-screen display, you need to use the getRotation() method to determine screen rotation, and then use the remapCoordinateSystem() method to map sensor coordinates to screen coordinates. You need to do this even if your manifest specifies portrait-only display.

http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html

like image 628
gurkan Avatar asked Dec 12 '22 13:12

gurkan


1 Answers

Found the solution.

The problem is caused by some newer devices' default orientation is set landscape while the others' portrait. So the sensor manager behaves accordingly. In order to get sensor manager work as you expect you need to detect the defaultDisplayRotation of the device and change the remapCoordinateSystem() parameter logic.

I have changed

SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);

to

int rotation = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(rotation == 0) // Default display rotation is portrait
    SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, R2);
else   // Default display rotation is landscape
    SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);

now it works... Hope this will be useful for someone else!

like image 87
gurkan Avatar answered Dec 21 '22 09:12

gurkan