Simple compass code with Android Studio

Android tutorial to learn how to build compass application in android studio.

Now in this blog post, I am going to make a very simple application which shows the direction like North, South, East, West. Exactly, I am going to build a simple compass application. I will show you the Java and XML code that will be used to build this simple compass application.

Before going to start I want to inform you that Some of the android devices do not have full support for motions sensors. So this application will not work for those types of android devices.

Code to build compass application in Android Studio

Compass image

Before you start coding in Android Studio you need a compass image which will rotate and show you the direction when your phone rotates. Here is the image I have used for the compass app:

Build Compass Application in Android Studio

You can also use the image in your compass app or use a different image.

Let’s coding

Now let’s start with coding. I am going to give you the full Java and XML code of this app.

Java code to create compass application in Android Studio

Here is the main Java code (

package com.codespeedy.compassbyeyeswift;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.hardware.SensorEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.hardware.SensorEventListener;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    // device sensor manager
    private SensorManager SensorManage;

    // define the compass picture that will be use
    private ImageView compassimage;

    // record the angle turned of the compass picture
    private float DegreeStart = 0f;

    TextView DegreeTV;

    protected void onCreate(Bundle savedInstanceState) {

        compassimage = (ImageView) findViewById(;

        // TextView that will display the degree
        DegreeTV = (TextView) findViewById(;

        // initialize your android device sensor capabilities
        SensorManage = (SensorManager) getSystemService(SENSOR_SERVICE);

    protected void onPause() {

        // to stop the listener and save battery

    protected void onResume() {

        // code for system's orientation sensor registered listeners
        SensorManage.registerListener(this, SensorManage.getDefaultSensor(Sensor.TYPE_ORIENTATION),

    public void onSensorChanged(SensorEvent event) {

        // get angle around the z-axis rotated
        float degree = Math.round(event.values[0]);

        DegreeTV.setText("Heading: " + Float.toString(degree) + " degrees");

        // rotation animation - reverse turn degree degrees
        RotateAnimation ra = new RotateAnimation(
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);

        // set the compass animation after the end of the reservation status

        // set how long the animation for the compass image will take place

        // Start animation of compass image
        DegreeStart = -degree;


    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // not in use

You need to replace the package name with your own. On Google play store each of the android application should have a unique package name just like domain names.

And here is the code for the main layout XML file (activity_main.xml):

<RelativeLayout xmlns:android=""
    android:background="#fff" >

        android:text="Heading: 0.0"
        android:textStyle="normal|bold" />

        android:src="@drawable/compass_icon" />


That’s all you need to create your compass app for Android. The image will show the direction and the TextView will display angle in degree with respect to North.

Now you can test your app. You can run your app or you can also build the apk or signed apk and then test it on your android device. See the picture below how the app will look:

build compass application android studio

Screenshot of compass application

Learn more,


One response to “Simple compass code with Android Studio”

  1. veerubha says:

    Not Supported in Some Marshmallow Devices…

    Please Help Me…

Leave a Reply

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