1. Are you ready for the Galaxy S20? Here is everything we know so far!

Displaying multiple sensors readings on screen

Discussion in 'Android Development' started by joeschmidt97, Oct 13, 2019.

  1. joeschmidt97

    joeschmidt97 Lurker
    Thread Starter

    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 (Text):
    1.  
    2. package com.example.sensorlistener_kotlin
    3.  
    4. import android.annotation.SuppressLint
    5. import android.content.Context
    6. import android.hardware.Sensor
    7. import android.hardware.SensorEvent
    8. import android.hardware.SensorEventListener
    9. import android.hardware.SensorManager
    10. import androidx.appcompat.app.AppCompatActivity
    11. import android.os.Bundle
    12. import kotlinx.android.synthetic.main.activity_main.*
    13.  
    14. class MainActivity : AppCompatActivity(), SensorEventListener {
    15.  
    16.    lateinit var sensorManager: SensorManager
    17.  
    18.    override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
    19.    }
    20.  
    21.    @SuppressLint("SetTextI18n")
    22.    override fun onSensorChanged(event: SensorEvent?) {
    23.  
    24.        label_accelerometerX.text = "X = ${event!!.values[0]}"
    25.        label_accelerometerY.text = "Y = ${event.values[1]}"
    26.        label_accelerometerZ.text = "Z = ${event.values[2]}"
    27.  
    28.    }
    29.  
    30.    override fun onCreate(savedInstanceState: Bundle?) {
    31.        super.onCreate(savedInstanceState)
    32.        setContentView(R.layout.activity_main)
    33.  
    34.        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    35.  
    36.        sensorManager.registerListener(
    37.            this,
    38.            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    39.            SensorManager.SENSOR_DELAY_NORMAL
    40.        )
    41.  
    42. /*        sensorManager.registerListener(
    43.            this,
    44.            sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
    45.            SensorManager.SENSOR_DELAY_NORMAL
    46.        )*/
    47.  
    48.    }
    49. }
    50.  
    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 (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     android:layout_margin="16dp"
    9.     tools:context=".MainActivity">
    10.  
    11.     <TextView
    12.         android:id="@+id/label_accelerometerX"
    13.         android:layout_width="wrap_content"
    14.         android:layout_height="wrap_content"
    15.         android:text="@string/accelerometerx"
    16.         app:layout_constraintTop_toTopOf="parent"
    17.         app:layout_constraintLeft_toLeftOf="parent" />
    18.  
    19.     <TextView
    20.         android:id="@+id/label_accelerometerY"
    21.         android:layout_width="wrap_content"
    22.         android:layout_height="wrap_content"
    23.         android:text="@string/accelerometery"
    24.         app:layout_constraintTop_toBottomOf="@id/label_accelerometerX"
    25.         app:layout_constraintLeft_toLeftOf="parent" />
    26.  
    27.     <TextView
    28.         android:id="@+id/label_accelerometerZ"
    29.         android:layout_width="wrap_content"
    30.         android:layout_height="wrap_content"
    31.         android:text="@string/accelerometerz"
    32.         app:layout_constraintTop_toBottomOf="@id/label_accelerometerY"
    33.         app:layout_constraintLeft_toLeftOf="parent" />
    34.  
    35.     <TextView
    36.         android:id="@+id/label_magneticX"
    37.         android:layout_width="wrap_content"
    38.         android:layout_height="wrap_content"
    39.         android:text="@string/magneticx"
    40.         app:layout_constraintTop_toBottomOf="@id/label_accelerometerZ"
    41.         app:layout_constraintLeft_toLeftOf="parent" />
    42.  
    43.     <TextView
    44.         android:id="@+id/label_magneticY"
    45.         android:layout_width="wrap_content"
    46.         android:layout_height="wrap_content"
    47.         android:text="@string/magneticy"
    48.         app:layout_constraintTop_toBottomOf="@id/label_magneticX"
    49.         app:layout_constraintLeft_toLeftOf="parent" />
    50.  
    51.     <TextView
    52.         android:id="@+id/label_magneticZ"
    53.         android:layout_width="wrap_content"
    54.         android:layout_height="wrap_content"
    55.         android:text="@string/magneticz"
    56.         app:layout_constraintTop_toBottomOf="@id/label_magneticY"
    57.         app:layout_constraintLeft_toLeftOf="parent" />
    58.  
    59. </androidx.constraintlayout.widget.ConstraintLayout>
    60.  
     

    Advertisement

    #1 joeschmidt97, Oct 13, 2019
    Last edited: Oct 14, 2019
  2. 23tony

    23tony Well-Known Member

    Can you share your XML too?
     
  3. joeschmidt97

    joeschmidt97 Lurker
    Thread Starter

    Just shared it!
     
    23tony likes this.
  4. 23tony

    23tony Well-Known Member

    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 (Text):
    1. label_accelerometerX.text = "X = ${event!!.values[0]}"
    2. 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...
     
Loading...

Share This Page

Loading...