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

Sensors

The document contains an XML layout file for an Android application with a vertical LinearLayout and a TextView to display a list of sensors. The MainActivity.java file initializes the SensorManager, retrieves all available sensors, and populates the TextView with their names. This setup allows the application to dynamically display sensor information on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sensors

The document contains an XML layout file for an Android application with a vertical LinearLayout and a TextView to display a list of sensors. The MainActivity.java file initializes the SensorManager, retrieves all available sensors, and populates the TextView with their names. This setup allows the application to dynamically display sensor information on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

activity_main.

xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<TextView
android:id="@+id/sensorslist"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Sensors" />
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

SensorManager sm = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
TextView tv = findViewById(R.id.sensorslist);

List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);


StringBuilder sb = new StringBuilder();

for (Sensor sensor : sensors) {


sb.append(sensor.getName()).append("\n");
}

tv.setText(sb.toString());
}
}

You might also like