Search code examples
javaandroidandroid-layoutproximitysensor

How to print values coming in logcat to UI of android?


I have created a proximity service which performs basic on/off of screen depending on the sensor value. I needed to time these sensor changes so i used the following code

    override fun onSensorChanged(event: SensorEvent) {
    val distance = event.values[0]
    val max = event.sensor.maximumRange
    val startTime = System.currentTimeMillis()

    if (distance < Math.min(max, 8.toFloat())) {
        listener.onNear()
    } else {
        listener.onFar()
    }

    val endTime = System.currentTimeMillis()
    System.out.println("That took " + (endTime - startTime) + " milliseconds")
}

I am getting the values in log cat but i need to print these on my app, my app activity is in another java class called settings activity. I have added a textview called "Time taken" how to push those values of (endTime-startTime) to this textview??? or should i have to use any other layout comonent?? My settings layout image


Solution

  • You need to set id of that TextView and then bindview in java and after it you can set text into TextView

    Your XML should have this widget

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    

    now in your java, bind this textview with id like

    TextView tv=(TextView)findViewById(R.id.textView1);
    

    now you have tv object of TextView that will help you to setText() like

    tv.setText("That took " + (endTime - startTime) + " milliseconds");