Detecting device orientation
Asked Answered
S

3

5

I need to detect android device orientation change without playing manually with sensor data, while keeping activity orientation stick to some orientation

onConfigurationChange will not work as will stick my activity to not rotate.

Playing around with sensor data to detect the orientation change I consider that as an invention of wheel, as android already does have embedded implementation of the algorithm to detect device orientation change. And from another side the detection of orientation change is not a simple checks like this.

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 3) {
            return;
        }
        //getResources().getConfiguration().orientation;
        if (event.values[1] < 6.5 && event.values[1] > -6.5) {
            if (orientation!=1) {
                Log.d("Sensor", "Protrait");
            }
            orientation=1;
        } else {
            if (orientation!=0) {
                Log.d("Sensor", "Landscape");
            }
            orientation=0;
        }
   }

It does really require real data processing like filtration from noise and sudden short shakes/rotations.

So any ideas how the device orientation can be detected using legacy services ? (Once again, I'll stick my activity to some orientation so onConfigurationChange will not work)

Stinkhorn answered 28/6, 2012 at 6:40 Comment(2)
is this not what you wanted??? #2796333Doroteya
no, I dont have any resource to be changedStinkhorn
M
11

There is a Listener for Orientation-Event.

Check the document here.

SO question mentioning implementation of that Listener.

Code Example for the same here in this blog

I hope this will help you

Mihalco answered 28/6, 2012 at 7:12 Comment(2)
Thanks man thats exactly what I needed !!! respect !!! I'm new android but seems lots of android-devs are not aware of this even here in stackoverflowStinkhorn
You welcome Deimus, No one is perfect, every one has its own domain of expertise, I am just started SO use for last 3-5 days :) Happy coadingMihalco
J
9

By setting screenOrientation property in Manifest you won't be able to get orientation value from onConfigurationChanged anymore as it would stop firing, it's possible to implement SensorEventListener to obtain Pitch, roll and yaw angles to calculate the orientation, but it's a bit complicated and an overkill for such a task. The best solution i found is to use OrientationEventListener, the following code will update rotation value on orientation change, the value is 0 to 4 according to the rotation angle:

private OrientationEventListener listener;
private int rotation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
        @Override
        public void onOrientationChanged(int orientation) {
            rotation = ((orientation + 45) / 90) % 4;
        }
    };
    if (listener.canDetectOrientation()) listener.enable();
    else listener = null; // Orientation detection not supported
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (listener != null) listener.disable();
}
Jarret answered 19/8, 2015 at 15:20 Comment(0)
F
6

My activity is set to always be in portrait mode. Put this code onCreate.

    myOrientationEventListener = new OrientationEventListener(this,SensorManager.SENSOR_DELAY_NORMAL) {
        public void onOrientationChanged(int arg0) {
            if (arg0>=315 || arg0<45){
                currentOrientation = Surface.ROTATION_90;
            }else if (arg0>=45 && arg0<135){
                currentOrientation = Surface.ROTATION_180;
            }else if (arg0>=135 && arg0<225){
                currentOrientation = Surface.ROTATION_270;
            }else if (arg0>=225 && arg0<315){
                currentOrientation = Surface.ROTATION_0;
            }
        }
    };
Fontaine answered 17/1, 2014 at 17:43 Comment(1)
This helped me a lot. When the Activity is locked to an orientation, but you still need the get device position this is the right way :)Killdeer

© 2022 - 2024 — McMap. All rights reserved.