Breaking News

Friday 14 March 2014

Android Compass Code Example

Today I’m going to share a working code to make a very simple compass application for your android device.
 
Some android device (like Huawei Y300 and Lenovo P700i) does not have full support of motions sensors so this code will not work for them.



Files Needed

You need to create your own compass image. For this example, I’m using a stock photo. Your image must be a PNG with transparent background, do not use this jpg file I used.


img_compass
Add caption

Let’s Code

Here’s our MainActivity.java
01package com.example.compassapp;
02
03import android.app.Activity;
04import android.hardware.Sensor;
05import android.hardware.SensorEvent;
06import android.hardware.SensorEventListener;
07import android.hardware.SensorManager;
08import android.os.Bundle;
09import android.view.animation.Animation;
10import android.view.animation.RotateAnimation;
11import android.widget.ImageView;
12import android.widget.TextView;
13
14public class MainActivity extends Activity implements SensorEventListener {
15
16    // define the display assembly compass picture
17    private ImageView image;
18
19    // record the compass picture angle turned
20    private float currentDegree = 0f;
21
22    // device sensor manager
23    private SensorManager mSensorManager;
24
25    TextView tvHeading;
26
27    @Override
28    protected void onCreate(Bundle savedInstanceState) {
29        super.onCreate(savedInstanceState);
30        setContentView(R.layout.activity_main);
31
32        //
33        image = (ImageView) findViewById(R.id.main_iv);
34
35        // TextView that will tell the user what degree is he heading
36        tvHeading = (TextView) findViewById(R.id.tvHeading);
37
38        // initialize your android device sensor capabilities
39        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
40    }
41
42    @Override
43    protected void onResume() {
44        super.onResume();
45
46        // for the system's orientation sensor registered listeners
47        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
48                SensorManager.SENSOR_DELAY_GAME);
49    }
50
51    @Override
52    protected void onPause() {
53        super.onPause();
54
55        // to stop the listener and save battery
56        mSensorManager.unregisterListener(this);
57    }
58
59    @Override
60    public void onSensorChanged(SensorEvent event) {
61
62        // get the angle around the z-axis rotated
63        float degree = Math.round(event.values[0]);
64
65        tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
66
67        // create a rotation animation (reverse turn degree degrees)
68        RotateAnimation ra = new RotateAnimation(
69                currentDegree,
70                -degree,
71                Animation.RELATIVE_TO_SELF, 0.5f,
72                Animation.RELATIVE_TO_SELF,
73                0.5f);
74
75        // how long the animation will take place
76        ra.setDuration(210);
77
78        // set the animation after the end of the reservation status
79        ra.setFillAfter(true);
80
81        // Start the animation
82        image.startAnimation(ra);
83        currentDegree = -degree;
84
85    }
86
87    @Override
88    public void onAccuracyChanged(Sensor sensor, int accuracy) {
89        // not in use
90    }
91}
Our layout file activity_main.xml
01<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02    android:layout_width="match_parent"
03    android:layout_height="match_parent"
04    android:background="#fff" >
05
06    <TextView
07        android:id="@+id/tvHeading"
08        android:layout_width="wrap_content"
09        android:layout_height="wrap_content"
10        android:layout_centerHorizontal="true"
11        android:layout_marginBottom="40dp"
12        android:layout_marginTop="20dp"
13        android:text="Heading: 0.0" />
14
15    <ImageView
16        android:id="@+id/imageViewCompass"
17        android:layout_width="wrap_content"
18        android:layout_height="wrap_content"
19        android:layout_below="@+id/tvHeading"
20        android:layout_centerHorizontal="true"
21        android:src="@drawable/img_compass" />
22
23</RelativeLayout>

Source Code Download

You can download this sample project here: CompassApp.zip

Some notes

My app orientation is locked to portrait mode. There are no special permissions in the Manifest file.
Read More
Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids