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)
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
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 (myOrientationEventListener.canDetectOrientation()) listener.enable();
else listener = null; // Orientation detection not supported
}
@Override
protected void onDestroy()
{
super.onDestroy();
if (listener != null) listener.disable();
}
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;
}
}
};