How to use gyroscope device sensors In Android

In this android programming source code example, we are going to use gyroscope device sensors In Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use gyroscope device sensors In Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity14">

    <TextView
        android:id="@+id/gyroscopeTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Gyroscope Text"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
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 com.bluapp.androidview2.R;

public class DeviceFeaturesActivity14 extends AppCompatActivity {
    private SensorManager sensorManager;
    private Sensor sensor;
    private TextView gyroscopeTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features14);
        gyroscopeTxt = (TextView) findViewById(R.id.gyroscopeTxt);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        if (sensor == null) {
            gyroscopeTxt.setText("No Gyroscope Sensor!");
        } else {
            sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if(event.values[2] > 0.5f) {
                getWindow().getDecorView().setBackgroundColor(Color.GREEN);
            } else if(event.values[2] < -0.5f) {
                getWindow().getDecorView().setBackgroundColor(Color.RED);
            }
        }
    };
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment