MainActivity.java

package com.example.appone;

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 androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity
        implements SensorEventListener {

    private TextView txtLight;
    private TextView txtPressure;
    private TextView txtHumidity;

    private SensorManager sensorManager;

    private Sensor lightSensor;
    private Sensor pressureSensor;
    private Sensor humiditySensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtLight = findViewById(R.id.txtLight);
        txtPressure = findViewById(R.id.txtPressure);
        txtHumidity = findViewById(R.id.txtHumidity);

        sensorManager =
                (SensorManager) getSystemService(SENSOR_SERVICE);

        lightSensor =
                sensorManager.getDefaultSensor(
                        Sensor.TYPE_LIGHT);

        pressureSensor =
                sensorManager.getDefaultSensor(
                        Sensor.TYPE_PRESSURE);

        humiditySensor =
                sensorManager.getDefaultSensor(
                        Sensor.TYPE_RELATIVE_HUMIDITY);

        if (lightSensor == null) {
            txtLight.setText("Not Available");
        }
        if (pressureSensor == null) {
            txtPressure.setText("Not Available");
        }
        if (humiditySensor == null) {
            txtHumidity.setText("Not Available");
        }
    }

    // FIX: register in onResume so updates continue after returning to the app
    @Override
    protected void onResume() {
        super.onResume();

        if (lightSensor != null) {
            sensorManager.registerListener(
                    this,
                    lightSensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
        if (pressureSensor != null) {
            sensorManager.registerListener(
                    this,
                    pressureSensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
        if (humiditySensor != null) {
            sensorManager.registerListener(
                    this,
                    humiditySensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {

            case Sensor.TYPE_LIGHT:
                txtLight.setText(
                        String.format(Locale.getDefault(), "%.2f Lux",
                                event.values[0]));
                break;

            case Sensor.TYPE_PRESSURE:
                txtPressure.setText(
                        String.format(Locale.getDefault(), "%.2f hPa",
                                event.values[0]));
                break;

            case Sensor.TYPE_RELATIVE_HUMIDITY:
                txtHumidity.setText(
                        String.format(Locale.getDefault(), "%.2f %% RH",
                                event.values[0]));
                break;
        }
    }

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


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="50dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Environment Sensor Dashboard"
            android:textSize="28sp"
            android:textStyle="bold"
            android:layout_marginBottom="24dp"/>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"

            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Light Sensor"
                    android:textStyle="bold"
                    android:textSize="20sp"/>

                <TextView
                    android:id="@+id/txtLight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Waiting for data..."
                    android:textSize="18sp"
                    android:layout_marginTop="8dp"/>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pressure Sensor"
                    android:textStyle="bold"
                    android:textSize="20sp"/>



                <TextView
                    android:id="@+id/txtPressure"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Waiting for data..."
                    android:textSize="18sp"
                    android:layout_marginTop="8dp"/>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Humidity Sensor"
                    android:textStyle="bold"
                    android:textSize="20sp"/>

                <TextView
                    android:id="@+id/txtHumidity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Waiting for data..."
                    android:textSize="18sp"
                    android:layout_marginTop="8dp"/>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

    </LinearLayout>

</ScrollView>