install android studio

How to make Compass app in Android

Compass android app:

Compass is the tool app which is used to determine the direction of the Android Device . Compass App also used to determine the value of inclination and declination of the android device.

Compass app tells the direction value in where ever you take the device. This app will use where it is found to be difficult in direction determination. In many phones , it is given as inbuilt app in tools app section. This can be made inĀ android studio by the following simple steps written below.

implementation :

To make the compass android app , there are only two steps . Follow the steps carefully


public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private ImageView imageView;
    private float[] mGravity = new float[3];
    private float[] mGeomagnetic = new float[3];
    private float azimuth = 0f;
    private float currectAzimuth = 0f;
    private SensorManager mSensorManager;
    protected void onCreate(Bundle savedInstanceState) {
        imageView = (ImageView)findViewById(;
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

    protected void onResume() {
        mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
        mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),

    protected void onPause() {
    public void onSensorChanged(SensorEvent sensorEvent) {
        final float alpha=  0.97f;
        synchronized (this){
            if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                mGravity[0] = alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
                mGravity[1] = alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
                mGravity[2] = alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
            if(sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
                mGeomagnetic[0] = alpha*mGeomagnetic[0]+(1-alpha)*sensorEvent.values[0];
                mGeomagnetic[1] = alpha*mGeomagnetic[1]+(1-alpha)*sensorEvent.values[1];
                mGeomagnetic[2] = alpha*mGeomagnetic[2]+(1-alpha)*sensorEvent.values[2];
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
                float orientation[] = new float[3];
                azimuth = (float)Math.toDegrees(orientation[0]);
                azimuth = (azimuth+360)%360;
                Animation anim = new RotateAnimation(-currectAzimuth,-azimuth,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                currectAzimuth = azimuth;
       } }
  public void onAccuracyChanged(Sensor sensor, int i){ }}

2. Activity_main.xml :

Just put the ImageView in activity_main.xml having image of Compass like this:

Problem will be discussed in the Comment Section.

About the author

Generic Bytes

Hi! we are from Generic Bytes an aspiring bloggers with an obsession of things tech.This blog is dedicated to people who want to learn the technology.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *