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)
getDefaultDisplay(). getRotation() returns the rotation with reference to the display's "natural" orientation, i.e. for a tablet it would return Surface. ROTATION_0 in landscape mode, whereas for a mobile phone it would return the same value for portrait.
Device motion and orientation events provide access to the built-in accelerometer, gyroscope, and compass in mobile devices. These events can be used for many purposes; in gaming, for example, to control the direction or action of a character.
Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change.
On the App. js file, you need to import the React Native Orientation Locker library and then import the following line of code: Orientation. lockToPortrait();
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 (listener.canDetectOrientation()) listener.enable();
else listener = null; // Orientation detection not supported
}
@Override
protected void onDestroy() {
super.onDestroy();
if (listener != null) listener.disable();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With