The degree of rotation is determined by the code float degree=Math.round(event.values) and is displayed on the TextView using the setText() method. In the above code, the SensorEvent is the parameter to the onSensorChanged() method. RotateAnimation ra= new RotateAnimation(currentDegree,-degree,Animation.RELATIVE_TO_SELF, 0. tText( " Rotation: "+Float.toString(degree)+ " degrees") Using the codeįollowing is the main layout file which defines a TextView control and an ImageView control in a relative void onSensorChanged(SensorEvent event)įloat degree=Math.round( event.values) In order for all this to work, the sensor manager of the device needs to be initialized when the app is initialized.Īlso, we need to register the orientation sensor of the device when the app is activated and unregister it when the app is paused, to conserve battery. Also, I have used an image of a compass to point to the North-South direction. In this article, I have used a TextView control to display the angle at which the device is oriented. I have used the following image from for my compass app: So be careful that you are holding the device away from any such signals to get accurate direction from the compass app. So if our mobile device does not have this sensor, a compass app cannot run on it.Īlso magnetometers are very sensitive to magnetic or electrical signals. The foremost thing to remember is that a compass app works by using the data provided by the magnetometer sensor on a mobile device. This article explains how we can create our own compass app using the built-in magnetic sensor of our mobile device.