0% found this document useful (0 votes)
13 views

Assignment 02

This document contains code for an Android application that uses the light sensor. The XML code defines the user interface with a TextView to display the light level readings. The Java code gets references to the SensorManager and light sensor, registers a sensor listener, and updates the TextView with light level values from the onSensorChanged method. When the sensor value changes, the light level is retrieved from the SensorEvent and displayed in the TextView.

Uploaded by

sher afzal
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)
13 views

Assignment 02

This document contains code for an Android application that uses the light sensor. The XML code defines the user interface with a TextView to display the light level readings. The Java code gets references to the SensorManager and light sensor, registers a sensor listener, and updates the TextView with light level values from the onSensorChanged method. When the sensor value changes, the light level is retrieved from the SensorEvent and displayed in the TextView.

Uploaded by

sher afzal
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

ASSIGNMENT

UNIVERSITY OF MIANWALI

Submitted By : Misbah
Tahir Roll no :
BSCSF20M024
Submitted To : Sir Faiz Ur Rehman
Submission Date : 18 June, 2023
Department of Computer Science,
UMW

XML code for activity_main.xml file:

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


<LinearLayout 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:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light Level: "
android:textSize="34dp"
android:textStyle="bold"/>
</LinearLayout>

JAVA Code for MainActivity.java file:

package com.firstapp.lightsensor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener

{ private SensorManager sensorManager;


private Sensor lightSensor;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//get reference to the view


textView = findViewById(R.id.txtValue);

//initialize sensorManager and LightSensor


sensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE); lightSensor =
sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}

@Override
protected void onResume()
{ super.onResume();
//Register sensor listener
sensorManager.registerListener(this, lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause()
{ super.onPause();
//unregistered the sensor listener
sensorManager.unregisterListener(this);

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_LIGHT)
{ float lightLevel = sensorEvent.values[0];
//update the light level textView
textView.setText("Light Level:"+ lightLevel);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//Do nothing
}
}

OUTPUT:
On Virtual Device Output:

You might also like