Sunday, August 8, 2010

OrientationEventListener, detect orientation change from the SensorManager.

android.view.OrientationEventListener is a Helper class for receiving notifications from the SensorManager when the orientation of the device has changed.

[Note: The another class OrientationListener is deprecated.]




The method onOrientationChanged(int orientation) will be called when the orientation of the device has changed. orientation parameter is in degrees, ranging from 0 to 359. orientation is 0 degrees when the device is oriented in its natural position, 90 degrees when its left side is at the top, 180 degrees when it is upside down, and 270 degrees when its right side is to the top. ORIENTATION_UNKNOWN is returned when the device is close to flat and the orientation cannot be determined.

Modify main.xml to have a TextView to display the updated orientation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/textorientation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation: "
/>
</LinearLayout>


AndroidOrientationSensor.java
package com.exercise.AndroidOrientationSensor;

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidOrientationSensor extends Activity{

TextView textviewOrientation;
OrientationEventListener myOrientationEventListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textviewOrientation = (TextView)findViewById(R.id.textorientation);

myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

@Override
public void onOrientationChanged(int arg0) {
// TODO Auto-generated method stub
textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
}};

if (myOrientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
finish();
}


}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myOrientationEventListener.disable();
}
}

Download the files.

Related Article: Detect rotation around X, Y & Z axis, using SensorManager and SensorEventListener

6 comments:

Unknown said...

Thank you so much

Anonymous said...

Works perfectly. Thanks!

Anonymous said...

Thanks lot. You helped me really. I was working with a custom camera application.
I have also referred the source code from
open camera project for a better idea.
http://opencamera.sourceforge.net/

Unknown said...

Thanks so much. This is what I'm looking for.

Unknown said...

Fantastic solution!!!!! THANKS!!!

Anonymous said...

2018 and working, thanks!