0% found this document useful (0 votes)
27 views4 pages

22 Sensor

This document contains an Android application code that utilizes a light sensor to display the current light level on the screen. It includes a layout with a TextView to show the sensor reading and a MainActivity class that implements SensorEventListener to handle sensor events. The app categorizes light levels into different messages based on the brightness detected by the sensor.

Uploaded by

ansamaan99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

22 Sensor

This document contains an Android application code that utilizes a light sensor to display the current light level on the screen. It includes a layout with a TextView to show the sensor reading and a MainActivity class that implements SensorEventListener to handle sensor events. The app categorizes light levels into different messages based on the brightness detected by the sensor.

Uploaded by

ansamaan99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

<?xml version="1.0" encoding="utf-8"?

>

<RelativeLayout

xmlns:android="[Link]

xmlns:app="[Link]

xmlns:tools="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<!-- Textview to show light sensor reading -->

<TextView

android:id="@+id/tv_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Light Sensor"

android:textSize="20sp"

android:textColor="@color/black"

android:layout_centerInParent="true" />

</RelativeLayout>

package [Link].gfg_sensor

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]
class MainActivity : AppCompatActivity(), SensorEventListener {

// Initialised sensorManager & two variables

// for storing brightness value

private lateinit var sensorManager: SensorManager

private var brightness: Sensor? = null

private lateinit var text: TextView

override fun onCreate(savedInstanceState: Bundle?) {

[Link](savedInstanceState)

setContentView([Link].activity_main)

// Set default nightmode

[Link](AppCompatDelegate.MODE_NIGHT_NO)

// searched our textview id and stored it

text = findViewById([Link].tv_text)

// setupSensor Called

setUpSensor()

// Declared setupSensor function

private fun setUpSensor() {

sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager

brightness = [Link](Sensor.TYPE_LIGHT)

// These are two methods from sensorEventListner Interface

override fun onSensorChanged(event: SensorEvent?) {

if (event?.sensor?.type == Sensor.TYPE_LIGHT) {
val light1 = [Link][0]

[Link] = "Sensor: $light1\n${brightness(light1)}"

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {

return

// Created a function to show messages according to the brightness

private fun brightness(brightness: Float): String {

return when ([Link]()) {

0 -> "Pitch black"

in 1..10 -> "Dark"

in 11..50 -> "Grey"

in 51..5000 -> "Normal"

in 5001..25000 -> "Incredibly bright"

else -> "This light will blind you"

// This is onResume function of our app

override fun onResume() {

[Link]()

[Link](this, brightness,
SensorManager.SENSOR_DELAY_NORMAL)

// This is onPause function of our app

override fun onPause() {


[Link]()

[Link](this)

You might also like