Android allows you to use the built-in orientation sensor. Thanks to this sensor we can determine the position of the phone – ie the top-down and left-right inclination. This allows you to program, for example, an electronic level. The same orientation sensor also allows you to specify the azimuth to which the phone is returned. In this example we will use all of these possibilities.

We start by sticking four elements of the TextView class on the screen:

We have to attach references to the sticky items to use them, nothing surprising here. The implementation of the SensorEventListener interface appears. What this is about? Our sensor will be constantly sending new readings to display these changes, we need to implement this interface. As a result, we will have to override the onSensorChanged and onAccuracyChanged methods. About this for a moment.

Before we start using our sensor, we need to register the selected class (it must implement the SensorEventListener interface) as the receiver for the sensor. I did this in the onCreate method of my activity, since this is run at startup. As a receiver, I registered the activity because I wanted to respond to changes in the readings (and I made sure that this action implements the SensorEventListener interface).

Due to the implementation of the SensorEventListener interface, I need to implement the onAccuracyChanged and onSensorChanged methods. The first one is called when the sensor’s accuracy changes (here we do not need it), the second one is called automatically when the sensor readings change (we are already interested). The readings from the sensor are passed to this method. Under index 0 (event.values [0]) we find the azimuth to which the top of the phone is pointing, under index 1 (event.values [1]) tilt in the up-down plane, under index 2 (event.values [2]) swing left right. The values of all three are listed on the first component.

Using these readings, I will display the appropriate comments on the other components. They will make the readings more readable. This is not a necessary element, pure aesthetics.
It would be worthwhile to add the support to the situation if the device does not have a sensor, not every phone is equipped with it.