How to Detect Phone Shakes Android Programming

You make have a project requirement to implement a feature that activates when the user Shakes his/her Android Phone. Say you want to reload the Activity contents when the user shakes the phone. To implement such functionality you need to create a listener that is related to motion and the hardware that deals with motion is called the Accelerator.

Accelerator is a motion sensor (hardware) that is built into your Android device that can measure motion and orientations. They provide us raw data with high precision and accuracy. Most of the Gaming Application makes use of this sensor.

Before we learn how to implement Shake functionality it is important to know that what minSDK level is Accelerator sensor available and does the device supports it. This sensor is available from early API Level 3 i.e. Android 1.5 Cupcake. Now let's see the code to detect if Accelerator is available on the device or not,

//This is a code to detect if ACCELEROMETER sensor is available on device or not

SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        List listOfSensorsOnDevice = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        for (int i = 0; i< listOfSensorsOnDevice.size(); i++) {
            if (listOfSensorsOnDevice.get(i).getType() == Sensor.TYPE_ACCELEROMETER) {

                Toast.makeText(this,"TYPE_ACCELEROMETER sensor is available on device",Toast.LENGTH_SHORT).show();
            else {

                Toast.makeText(this,"TYPE_ACCELEROMETER sensor is NOT available on device",Toast.LENGTH_SHORT).show();

SensorManager is the class that we have to make use of to detect sensor availability. Just add the above code in the onCreate() function of the launcher Activity it check if ACCELEROMETER sensor is available.

Note : ACCELEROMETER sensor is not available on Emulator so do check this code on your handheld device.

Example: Detect Android Shake event

Let's take a very simple example, We will create a simple application with only one Activity containing a TextView. When the user shakes the phone the TextView text is updated to display the count i.e. number of times the shake event occurred.

File : activity_main.xml
<RelativeLayout xmlns:android=""
    xmlns:tools="" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/counter" android:layout_width="wrap_content"
        android:id="@+id/counter" />

File :

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

import java.util.List;

public class MainActivity extends ActionBarActivity implements SensorEventListener {

    int count = 1;
    private boolean init;
    private Sensor mAccelerometer;
    private SensorManager mSensorManager;
    private float x1, x2, x3;
    private static final float ERROR = (float) 7.0;

    private TextView counter;

    protected void onCreate(Bundle savedInstanceState) {

        counter = (TextView) findViewById(;
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        List listOfSensorsOnDevice = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        for (int i = 0; i < listOfSensorsOnDevice.size(); i++) {
            if (listOfSensorsOnDevice.get(i).getType() == Sensor.TYPE_ACCELEROMETER) {

                Toast.makeText(this, "ACCELEROMETER sensor is available on device", Toast.LENGTH_SHORT).show();

                init = false;

                mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

            } else {

                Toast.makeText(this, "ACCELEROMETER sensor is NOT available on device", Toast.LENGTH_SHORT).show();


    public void onSensorChanged(SensorEvent e) {

        //Get x,y and z values
        float x,y,z;
         x = e.values[0];
         y = e.values[1];
         z = e.values[2];

        if (!init) {
            x1 = x;
            x2 = y;
            x3 = z;
            init = true;
        } else {

            float diffX = Math.abs(x1 - x);
            float diffY = Math.abs(x2 - y);
            float diffZ = Math.abs(x3 - z);

            //Handling ACCELEROMETER Noise
            if (diffX < ERROR) {

                diffX = (float) 0.0;
            if (diffY < ERROR) {
                diffY = (float) 0.0;
            if (diffZ < ERROR) {

                diffZ = (float) 0.0;

            x1 = x;
            x2 = y;
            x3 = z;

            //Horizontal Shake Detected!
            if (diffX > diffY) {

                counter.setText("Shake Count : "+ count);
                count = count+1;
                Toast.makeText(MainActivity.this, "Shake Detected!", Toast.LENGTH_SHORT).show();


    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //Noting to do!!

    //Register the Listener when the Activity is resumed
    protected void onResume() {
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    //Unregister the Listener when the Activity is paused
    protected void onPause() {
Related Questions
  1. How to detect Horizontal shake motions in Android?
  2. Detect Shakes using Android Accelerometer?
  3. Android Shake detector source code?
  4. How to check if Accelerometer is available on Android device ?
  5. Detect Shake Gesture in Android?

HashTags : #Android #Shake #ShakeDetection #MotionSensors #Sensors #Accelerometer

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap