Create a Simple Pedometer and Step Counter in Android

Pedometer

Anu S Pillai

The Admin at GadgetSaint.com

You may also like...

56 Responses

  1. Royal Lachinov says:

    This code is not working. There is lack og textview.setText()

  2. Royal Lachinov says:

    Should we declare permission in the Manifest file for run this app

  3. Anu S Pillai says:

    No permissions required

  4. Anu S Pillai says:

    Can you tell where do we need textview.settext?

  5. Anonymous says:

    I implemented it in my app and it works perfectly! Thanks so much!

  6. john says:

    why i cant insert data in my sql lite ? my app crash

  7. Anu S Pillai says:

    What error are you getting? Share your logcat

  8. jeffrey says:

    I don’t understand SensorFilter.java why are you used them?

  9. Anu S Pillai says:

    As the name says, its used for filtering out unwanted values from accelerometer data

  10. France says:

    Is it possible to make public step listener data to string?

  11. Anu S Pillai says:

    Yes. You make cast the value to String.

  12. rahul ahuja says:

    the app is not working … there is no error but the app is crashing .. what to do next ?????

  13. Anu S Pillai says:

    Can you post the logcat?

  14. rahul ahuja says:

    now error is coming cannot resolve symbol tvsteps???

  15. Anu S Pillai says:

    Tvsteps is the textview which you need to link with your layout xml using findviewbyid

  16. rahul ahuja says:

    when im declaring it with textview then it is coming tvsteps is not used

  17. Anu S Pillai says:

    I think I need to see your code.

  18. Zain says:

    can’t the apps stored the walked data before on Firebase ?

  19. Anu S Pillai says:

    I can’t get you. Do you mean you want to write step count data in Firebase? If that is the case you can do it. https://firebase.google.com/docs/database/android/read-and-write

  20. Zain says:

    Yes, write the step count data on Firebase. How I can do this ? I mean the step are saved in Firebase

  21. Joe says:

    Hi! Can i convert lister.step as model class ?

  22. alex says:

    Hello just a question, thanks for the code i tried it but can you explain me a little how it’s works ?
    Also this code is not very sensitive how can i increase sensibility ? Did I have to increase or decrease the treeshold ?

  23. Anu S Pillai says:

    Decrease the threshold to increase the sensitivity.

  24. alex says:

    I will try thank you, also this code works, can you explain me the principe? It’s a part of my thesis , i can quote you if you want 🙂 .

  25. Anonymous says:

    Hi, step(long timeNs) method not calling in MainActivivty. Any solution for this.

  26. Anonymous says:

    Yo man,
    thanks 4 the example, it works!

  27. kingslanding1 says:

    STEP_THRESHOLD and STEP_DELAY_NS are not appropriate for step detect. Step increases when hang the phone up and down.

  28. Vishnu says:

    Great work. How do you calculate distance from these steps?

  29. shanks says:

    I implemented its working . Thank you so much.

  30. Tanvi Sharma says:

    Hello Sir, Thanks for posting this code.
    but i’m having an error in my code in Tvstep statement (at the last of main activity)
    plz help!

  31. Anu S Pillai says:

    What error are you getting?

  32. Tanvi Sharma says:

    in error its saying Tvsteps can not be resolved.

    plz can i get ur emailid? ill send u screenshots, then u can check in that.

  33. Tanvi Sharma says:

    there is no other error except this

  34. Anu S Pillai says:

    You are getting this error because TvSteps is a TextView which need to be initialised in your onCreate using “findviewbyid”. You need to link your TvSteps TextView with the XML layout.

  35. Anu S Pillai says:

    Do something like this in your activity onCreate:

    TextView TvSteps = (TextView) findviewbyid(R.id.yourtextviewidinxml); // replace with your TextView id defined in layout XML

  36. Tanvi Sharma says:

    //please check the code
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Two extends ActionBarActivity implements StepListener,SensorEventListener {

    private TextView textView;
    private StepDetector simpleStepDetector;
    private SensorManager sensorManager;
    private Sensor accel;
    private static final String TEXT_NUM_STEPS = “Number of Steps: “;
    private int numSteps;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    // Get an instance of the SensorManager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    simpleStepDetector = new StepDetector();
    simpleStepDetector.registerListener(this);

    //Tvsteps is initialised here
    TextView TvSteps = (TextView) findViewById(R.id.tv_steps);
    Button BtnStart = (Button) findViewById(R.id.btwo1);
    Button BtnStop = (Button) findViewById(R.id.btwo2);

    BtnStart.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    numSteps = 0;
    sensorManager.registerListener(Two.this, accel, SensorManager.SENSOR_DELAY_FASTEST);

    }
    });

    BtnStop.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    sensorManager.unregisterListener(Two.this);

    }
    });

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    simpleStepDetector.updateAccel(
    event.timestamp, event.values[0], event.values[1], event.values[2]);
    }
    }

    @Override
    public void step(long timeNs) {
    numSteps++;

    //error in this Tvsteps
    TvSteps.setText(TEXT_NUM_STEPS + numSteps);

    }

    }

    //there r no errors in xml layput

  37. Tanvi Sharma says:

    //i have taken the same name in my xml layout, see:

  38. Tanvi Sharma says:
  39. Anu S Pillai says:

    Make your TextView TvSteps global variable, declare it outside onCreate

    TextView TvSteps;

    OnCreate{

    TvSteps = (TextView) findViewById(R.id.tv_steps);

    }

  40. Tanvi Sharma says:

    By this that error is removed in TvSteps … Thanks!
    but a new problem has occured,
    its saying:

    Sytax error on token “OnCreate”, delete this token.

  41. Anu S Pillai says:

    Share your entire code again

  42. Anu S Pillai says:

    I think you copy pasted the code I send you……Keep your onCreate the way it is…..just make your TextView global…..the code I send is just an example for your understanding

  43. Tanvi Sharma says:

    WoW!! thankyou so muchh … no errors now finally 🙂

    Bdw why was it showing error when i declared it inside OnCreate?
    this is a right way, then wht was the need to declare it as global?? we didnt declare button as global but its still working.. then what was the problem?

  44. Anu S Pillai says:

    When you declare inside onCreate it’s a local variable to onCreate and it’s scope is limited to onCreate. Declaring global you can access it from anywhere in the class. Here you are trying to access the local variable of onCreate outside….that’s why it’s showing error. By the way you need to work on your basics.

  45. Tanvi Sharma says:

    Sir,one more problem … there r no errors in the code now.
    but the application is not working on d phone. i mean, its opening up, but the buttons r not working, no steps r being counted, though the code is complete n corrct

  46. Tanvi Sharma says:

    n yes sir, ill revise the basics, but right now plz help

  47. Charlie Simmons says:

    Is it possible for the app to still count steps even when the phone is locked?

  48. ali says:

    Hy Anu! Why did not you used Step_count_sensor and step_detect_sensor? What is the best practice using those sensors or using own logic?

  49. Anu S Pillai says:

    Most of the Android devices doesn’t have an inbuilt Step Counter Sensor. In that case use my code. My code works on accelerometer.

  50. Hey man,
    I would love if you commented your code and possibly tossed it on github 🙂

  51. Christian says:

    This is not working for me 🙁

  52. Risca says:

    Do you have any references from where you got the formulas in SensorFilter class? Would you give me some information please..

  53. Pankaj says:

    This code is very helpful for me.
    sir in future , can i ask any question regarding to this code and about pedoMeter

  54. Pankaj says:

    In this code ,do you have any permission required in manifest

Leave a Reply

Your email address will not be published.