0% found this document useful (0 votes)
5 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)
5 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
You are on page 1/ 4

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

>

<RelativeLayout

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"

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 com.mrtechy.gfg_sensor

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 android.widget.TextView

import androidx.appcompat.app.AppCompatDelegate
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?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

// Set default nightmode

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

// searched our textview id and stored it

text = findViewById(R.id.tv_text)

// setupSensor Called

setUpSensor()

// Declared setupSensor function

private fun setUpSensor() {

sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager

brightness = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)

// These are two methods from sensorEventListner Interface

override fun onSensorChanged(event: SensorEvent?) {

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

text.text = "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 (brightness.toInt()) {

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() {

super.onResume()

sensorManager.registerListener(this, brightness,
SensorManager.SENSOR_DELAY_NORMAL)

// This is onPause function of our app

override fun onPause() {


super.onPause()

sensorManager.unregisterListener(this)

You might also like