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

Android_Sensor_List_Display

The document describes an Android application that displays a list of available sensors on a mobile device. It includes a Java file (MainActivity.java) that retrieves sensor names using the SensorManager and populates a ListView with these names. The XML layout (activity_main.xml) defines the user interface for displaying the sensor list.

Uploaded by

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

Android_Sensor_List_Display

The document describes an Android application that displays a list of available sensors on a mobile device. It includes a Java file (MainActivity.java) that retrieves sensor names using the SensorManager and populates a ListView with these names. The XML layout (activity_main.xml) defines the user interface for displaying the sensor list.

Uploaded by

omyenpure28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Android Sensor List Display

Java File: MainActivity.java

// MainActivity.java
package com.example.sensordisplay;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ListView sensorListView;

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

sensorListView = findViewById(R.id.sensorListView);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);


ArrayList<String> sensorNames = new ArrayList<>();

for (Sensor sensor : sensorList) {


sensorNames.add(sensor.getName());
}

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, sensorNames);
sensorListView.setAdapter(adapter);
}
}

XML File: activity_main.xml

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<ListView
android:id="@+id/sensorListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

Expected Output

Expected Output:
The application displays a list of sensors available on the mobile device, such as:
- Accelerometer
- Gyroscope
- Magnetometer
- Proximity Sensor
- Light Sensor
- Gravity Sensor

You might also like