THE COMPASS : Android Studio

We‟ll develop a simple compass app that will utilize the internal accelerometer and magnetometer sensors of the Android device. Accelerometer is a sensor which converts the mechanical acceleration information to electrical signals and similarly a magnetometer is used to translate the magnetic field intensity to electronic signals.

As we develop our compass app, we‟ll learn setting permissions to use sensors, reading acceleration and magnetic field data in Java code, extracting the orientation data from the sensor data and animating images. In the end, we‟ll have a complete compass app that we can use in daily life.

Setting up the Required Permissions

Let‟s start by creating an Android project first. I named the project as Compass App and selected Empty Activity as the default activity type. The minimum API is also set to 15.We‟ll need a compass image whose needle shows the absolute north. I found the royalty free image shown in Figure for this aim.Please copy and paste this image to your drawable folder as we did before. The name of the image is compass.png, we‟ll use its name to access it in our code.

If we use sensors in an Android project, we have to get the required permissions to use these sensors in the AndroidManifest.xml file which is located in the manifests folder as shown below:

Open this file by double clicking on it in Android Studio and you‟ll see its default contents as shown in above Figure. Please add the lines shown in Code 1to this file before the tag and you‟ll obtain the finalized contents as shown in Code 2 and Code 3 . These lines make the accelerometer and magnetometer outputs available to be used in our app.

Code 1
Code 2
Code 3

Designing the GUI of the App

Now, let‟s design the layout of the app. Please open the layout_main.xml file for this and change the text of the default Hello World TextView to Compass App which will serve as the app title. Please set its font size as 30sp and bold style. Then, please position it as follows:

Let‟s now place an ImageView in the middle of the GUI and select the compass image that we pasted to the drawable folder:

After we place the ImageView, it‟ll be selected. Then, please set up its ID as iv_compass (short for ImageView_compass) from the right pane of Android Studio as follows:

Finally, let‟s place a TextView below the ImageView in which we‟ll display the orientation angle in real time. I set its ID as tv_degrees, and made it 24sp with a bold text as shown below:

Writing the Main Code of the App

Building and Running the App

If we try to run the app in an emulator, the compass will constantly show the north and the azimuth angle as 0 degrees. We need to try this app on a real device with a magnetometer and accelerometer inside (most Android devices have). Please build the app in Android Studio and install it on a real device.

THE COMPASS : Android Studio

Now, let‟s take a break and get a strong coffee. In the next chapter, we‟ll learn using GPS and maps in our app.

For now you can check some awesome tutorials Here

Leave a Reply

Your email address will not be published.