2. Once the project is created open your activity_main.xml file and Add Below Code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="64dp"
android:paddingRight="64dp"
android:paddingTop="64dp"
android:paddingBottom="64dp"
tools:context="com.targetandroid.info.sensorlist.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lvsensors">
</ListView>
</RelativeLayout>
3. Now Open Your MainActivity.java
sensors
Add Below Code In File
package com.targetandroid.info.sensorlist;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class SensorsActivity extends AppCompatActivity {
//Creating Variable
ListView lvsensors;
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvsensors=(ListView)findViewById(R.id.lvsensors);
mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
//Getting the list of all sensors supported by device
List<Sensor> sList=mSensorManager.getSensorList(Sensor.TYPE_ALL);
List<String> namelist= new ArrayList<>();
//put all the name of sensors into new List<String>
for(int i=0; i<sList.size();i++){
namelist.add(sList.get(i).getName());
}
ArrayAdapter listadapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,namelist);
lvsensors.setAdapter(listadapter);
}
}
4. Now finally run Your Project
Thank You
0 comments:
Post a Comment