A Compass Application in Android

Posted on Apr 10, 2010 (4 years ago). Seen 21,492 times. 3 comments. Permalink Feed
Photo Marko Gargenta
@MarkoGargenta
Member since Jan 19, 2007
Location: San Francisco
Stream Posts: 42
Tagged as: Android Tutorial

This is a little example on how to write a simple compass application in Android. It illustrates two points:

  • How to use Sensors
  • How to create custom Views (widgets)

The Activity: src/com/marakana/Compass.java

This Activity is using the SensorManager to register for sensor updates. Notice that we're using an older API. This is on purpose so that it runs on my 1.5 HTC phone.


Code:

package com.marakana;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

// implement SensorListener
public class Compass extends Activity implements SensorListener {
SensorManager sensorManager;
static final int sensor = SensorManager.SENSOR_ORIENTATION;
Rose rose;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);

rose = new Rose(this);

setContentView(rose);

// get sensor manager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

// register to listen to sensors
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor);
}

// unregister
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}

// Ignore for now
public void onAccuracyChanged(int sensor, int accuracy) {
}

// Listen to sensor and provide output
public void onSensorChanged(int sensor, float[] values) {
if (sensor != Compass.sensor)
return;
int orientation = (int) values[0];
rose.setDirection(orientation);
}
}

The Custom View src/com/marakana/Rose.java

This is the custom view that we create to implement the compass rose. We simply subclass ImageView, override the onDraw() method, and provide some logic to draw the image and rotate it based on what orientation sensor tells us to.

Code:

package com.marakana;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.widget.ImageView;

public class Rose extends ImageView {
Paint paint;
int direction = 0;

public Rose(Context context) {
super(context);

paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(2);
paint.setStyle(Style.STROKE);

this.setImageResource(R.drawable.compassrose);
}

@Override
public void onDraw(Canvas canvas) {
int height = this.getHeight();
int width = this.getWidth();

canvas.rotate(direction, width / 2, height / 2);
super.onDraw(canvas);
}

public void setDirection(int direction) {
this.direction = direction;
this.invalidate();
}

}



The Output


Source

http://thenewcircle.com/static/tutorials/Compass.zip
http://thenewcircle.com/static/tutorials/Compass.apk

Comments

Posted on Jun 13, 2011
Photo Ziyauddin Azmi
Slktechlabs
Member since Jun 12, 2011
I m quite new to Android and still learning things. I have a doubt regarding the sensors. Is there any relation between sensors and views in Android? Can anyone please clear my doubt Thanks a lot.
Posted on Jun 14, 2011
Photo Naves T
Android
Member since Jun 14, 2011
Hey thank you for the source...I am new to developing android apps and I was wondering can I run this source code as a .html file in chrome to test it out?

thank you
Posted on Jul 11, 2011
Photo Marc Bernardo
Studant
Studant
Member since Jul 11, 2011
This work is deprecated...Beacuse of the SensorEvent, now it is used the SensorEventListener*... Can someone help me changing this code for the new Sensor?

Thanks