Android SDK Tutorial: Accelerometer Example

This is am going to teach to make is so simple but I could be playing with it for hours, just think of all the possibilities.

The Result

We will make a simple app, one that shows you the acceleration of your phone/tablet in all axes. The acceleration won’t be zero when your are not moving though but the numbers for each axis will change when you tilt your device in any direction.

Android app showing accelration in all 3d directions

Android app showing accelration in the x y and z axes.

The Java Code(HelloAndroid.java)

package com.example.helloandroid;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity implements SensorEventListener {
	private SensorManager sensorManager;
	
	TextView xCoor; // declare X axis object
	TextView yCoor; // declare Y axis object
	TextView zCoor; // declare Z axis object
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
		yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
		zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object
		
		sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
		// add listener. The listener will be HelloAndroid (this) class
		sensorManager.registerListener(this, 
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		
		/*	More sensor speeds (taken from api docs)
		    SENSOR_DELAY_FASTEST get sensor data as fast as possible
		    SENSOR_DELAY_GAME	rate suitable for games
		 	SENSOR_DELAY_NORMAL	rate (default) suitable for screen orientation changes
		*/
	}

	public void onAccuracyChanged(Sensor sensor,int accuracy){
		
	}
	
	public void onSensorChanged(SensorEvent event){
		
		// check sensor type
		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
			
			// assign directions
			float x=event.values[0];
			float y=event.values[1];
			float z=event.values[2];
			
			xCoor.setText("X: "+x);
			yCoor.setText("Y: "+y);
			zCoor.setText("Z: "+z);
		}
	}
}
	 

The Layout(main.xml)

Just three text labels, one for each axis.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout	
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<TableRow>
	<TextView 
		android:id="@+id/xcoor"
		android:text="X Coordinate: "
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
	/>
	</TableRow>
	
	<TableRow>
	<TextView 
		android:id="@+id/ycoor"
		android:text="Y Coordinate: "
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
	/>
	</TableRow>
	
	<TableRow>
	<TextView 
		android:id="@+id/zcoor"
		android:text="Z Coordinate: "
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
	/>
	</TableRow>
		
</TableLayout>

The Manifest(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.helloandroid"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="13" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    
        <activity android:name=".HelloAndroid"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>