• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Displaying multiple sensors readings on screen

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
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:

upload_2019-10-12_23-26-16.png


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:
Does Kotlin directly reference the objects? In Java, I have to use findViewById() to get the view object in order to change anything. I don't see anything like that in your code.

Also, your code is different for one of the sensors vs the other two shown:
Code:
label_accelerometerX.text = "X = ${event!!.values[0]}"
label_accelerometerY.text = "Y = ${event.values[1]}"
- is this a Kotlin thing where the first reference is event!! and subsequent references are just event?

Maybe I should bow out in favor of someone more versed in Kotlin...
 
Back
Top Bottom