joeschmidt97
Lurker
Hello,
I am trying to display multiple sensor readings on screen (accelerometer, magnetometer, etc.) in its own line of text. I can change the text onscreen for one type of sensor (accelerometer), but not another sensor. I registered both sensors as Listeners, but don't know how to update each one separately inside the onSensorChanged function. This has to be done in Kotlin, not Java.
MainActivity.kt
As the end result, I want to display X,Y,Z components of accelerometer, magnetometer, and gyroscope on screen in their own line as follows:
activity_main.xml
I am trying to display multiple sensor readings on screen (accelerometer, magnetometer, etc.) in its own line of text. I can change the text onscreen for one type of sensor (accelerometer), but not another sensor. I registered both sensors as Listeners, but don't know how to update each one separately inside the onSensorChanged function. This has to be done in Kotlin, not Java.
MainActivity.kt
Code:
package com.example.sensorlistener_kotlin
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), SensorEventListener {
lateinit var sensorManager: SensorManager
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}
@SuppressLint("SetTextI18n")
override fun onSensorChanged(event: SensorEvent?) {
label_accelerometerX.text = "X = ${event!!.values[0]}"
label_accelerometerY.text = "Y = ${event.values[1]}"
label_accelerometerZ.text = "Z = ${event.values[2]}"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(
this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL
)
/* sensorManager.registerListener(
this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL
)*/
}
}
As the end result, I want to display X,Y,Z components of accelerometer, magnetometer, and gyroscope on screen in their own line as follows:
activity_main.xml
Code:
<?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"
android:layout_margin="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/label_accelerometerX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accelerometerx"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/label_accelerometerY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accelerometery"
app:layout_constraintTop_toBottomOf="@id/label_accelerometerX"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/label_accelerometerZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accelerometerz"
app:layout_constraintTop_toBottomOf="@id/label_accelerometerY"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/label_magneticX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/magneticx"
app:layout_constraintTop_toBottomOf="@id/label_accelerometerZ"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/label_magneticY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/magneticy"
app:layout_constraintTop_toBottomOf="@id/label_magneticX"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/label_magneticZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/magneticz"
app:layout_constraintTop_toBottomOf="@id/label_magneticY"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Last edited: