0

Is there a way to read or calculate the G-Force from my device into a TextView using Android Studio?

Galactix
  • 1
  • 3

1 Answers1

0

I'm assuming you mean the accelerometer? It's built into phones to measure movement on the X-, Y-, and Z-axes. It certainly is possible to display these on a TextView. To do so, you want to first declare a SensorManager and a Sensor in your MainActivity class like so:

private SensorManager SensorManager1;
private Sensor AccelSensor;

SensorManager basically allows you to access the device's sensors and the Sensor itself is there to retrieve data from a sensor of your choice.

In your OnCreate(), you can set them up.

SensorManager1 = ( SensorManager ) getSystemService( SENSOR_SERVICE );

Once assigned to the system's sensors, you can now continue to set up your individual sensor.

AccelSensor = SensorManager1.getDefaultSensor( Sensor.TYPE_ACCELEROMETER );

AccelSensor is now set up to receive data from the phone's accelerometer, however, it is not yet connected to the SensorManager, which means that it has no access to the readings.

SensorManager1.registerListener( this, AccelSensor, SensorManager.SENSOR_DELAY_NORMAL );

To do so, connect it to the SensorManager with registerListener().

Now, you will get a red underline on the above code. Don't panic, you just haven't implemented the required methods yet. We are using "this" to refer to the SensorEventListener, but Android Studio does not see it. To do so, take your public class MainActivity extends AppCompatActivity and add implements SensorEventListener to it. It should end up being:

public class MainActivity extends AppCompatActivity implements SensorEventListener {
...
}

Now the red line from earlier is gone, but a new one is here! All you have to do now is right-click the implements SensorEventListener and Android Studio should give you an option to Implement Methods. Click that and implement onSensorChanged() and onAccuracyChanged(). You will now see two new functions pop up in your code.

We don't need onAccuracyChanged, so just leave it be:

@Override
    public void onAccuracyChanged( Sensor sensor, int accuracy ) {
        // unused, but android studio needs this here
    }

onSensorChanged is what we need. This is what is called every time the sensor updates, or changes. Here is where we can update the TextViews from earlier to show the values.

SensorEvent's event is an array of 3 values. These three values are the X, Y, and Z readings of the accelerometer and are located at indices 0, 1, and 2 respectively. So, we can take those and simply concatenate that with a letter (to show which reading is which) like so:

This should display each value as "[X/Y/Z] = [value]"

@Override
    public void onSensorChanged( SensorEvent event ) {
        ViewX.setText( "X = " + event.values[ 0 ] );
        ViewY.setText( "Y = " + event.values[ 1 ] );
        ViewZ.setText( "Z = " + event.values[ 2 ] );
    }

And that's it! You now have a working sensor to view readings from the accelerometer and they will be displayed on the TextViews.