title: How to Implement Step Counter in Android Studio If Pedometer Sensor Not Available
body: <p>I have a Samsung phone which apparently doesn't have a Pedometer (step counter) because when I use TYPE_STEP_COUNTER it says doesn't have one. So after searching online, I came across an implementation using accelerometer censor. But the problem is the implementation is not very accurate at detecting steps. I have tried to calibrate the numbers but still not very reliable. It will add the squares of x, y, z axis and calculate the root to get the magnitude of the steps. So my question is has anyone done this and how they did it in a way that is a good implementation. Thanks
  here's the code I'm using</p>
  
  <pre><code>public class StatsFragment extends Fragment implements SensorEventListener {
      private static final String LOG_TAG = "STATS";
      private TextView stepCountTextView;
      private Button clearCounterButton;
      private Sensor stepSensor;
      private Integer stepCount = 0;
      private double MagnitudePrevious = 0;
      private SensorManager sensorManager;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                               @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_stats, container, false);
  
          stepCountTextView = view.findViewById(R.id.step_counter);
          clearCounterButton = view.findViewById(R.id.clear_button);
  
          sensorManager = (SensorManager) getActivity().getSystemService(getActivity().SENSOR_SERVICE);
          stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  
          clearCounterButton.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View view) {
                  stepCount = 0;
                  stepCountTextView.setText(stepCount.toString());
              }
          });
  
          return view;
      }
  
      @Override
      public void onResume() {
          super.onResume();
          if(stepSensor != null) {
              sensorManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI);
          }
          else {
              Toast.makeText(getActivity(), "Sensor not available!", Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      public void onPause() {
          super.onPause();
          //unregister sensor
          sensorManager.unregisterListener(this);
      }
  
      @Override
      public void onSensorChanged(SensorEvent sensorEvent) {
          if (sensorEvent != null) {
              float x_acceleration = sensorEvent.values[0];
              float y_acceleration = sensorEvent.values[1];
              float z_acceleration = sensorEvent.values[2];
              double Magnitude = Math.sqrt(x_acceleration*x_acceleration + y_acceleration*y_acceleration
                      + z_acceleration*z_acceleration);
              double MagnitudeDelta = Magnitude - MagnitudePrevious;
              MagnitudePrevious = Magnitude;
  
              if (sensorEvent.values[0] &gt; 6){
                  stepCount++;
              }
              stepCountTextView.setText(stepCount.toString());
          }
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int i) {
  
      }
  }
  </code></pre>