---
title: "Sensor in Android"  
description: "Hi everyone in this article I’m explaining about Sensor in Android"  
author: "Manoj Pandey"  
published: 2015-03-11  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1647/sensor-in-android  
category: "android"  
tags: ["android"]  
reading_time: 2 minutes  

---

# Sensor in Android

Use sensors on the device to add rich [location](https://www.mindstick.com/news/2574/twitter-conveniently-reveals-a-location-sharing-policy-amid-elonjet-controversy) and motion [capabilities](https://www.mindstick.com/interview/490/explain-the-concepts-and-capabilities-of-assembly-in-dot-net) to [your app](https://answers.mindstick.com/qa/33368/how-will-you-design-a-listview-which-downloads-a-lot-of-images-and-displays-in-it-without-getting-your-app-hanged-whats-the-best-possible-way), from GPS or network location to accelerometer, gyroscope, [temperature](https://www.mindstick.com/blog/300335/what-is-normal-body-temperature), barometer, and more.\

Most of the android devices have built-in sensors that measure motion, [orientation](https://www.mindstick.com/interview/2604/what-is-orientation), and various [environmental](https://answers.mindstick.com/qa/52071/who-has-been-awarded-the-2019-goldman-environmental-prize) condition. The android [platform](https://www.mindstick.com/articles/13080/wikipedia-the-most-powerful-advertising-platform-of-the-digital-age) [supports](https://www.mindstick.com/blog/12043/how-to-create-a-killer-mobile-app-that-supports-your-brand) three broad [categories](https://answers.mindstick.com/qa/47601/what-are-the-categories-of-stations-for-provision-of-passenger-amenities-explain-them) of sensors.

· Motion Sensors

· Environmental sensors

· Position sensors

Sensors can be used to monitor the three-dimensional device movement or change in the [environment](https://answers.mindstick.com/qa/38115/according-to-the-report-released-by-the-ministry-of-environment-forests-and-climate-change-on-august-12-what-is-the-total-estimated-number-of-elephants-in-india) of the device.

##### *Android provides sensor api to work with different types of sensors.*

![Sensor in Android](https://www.mindstick.com/mindstickarticle/48327284-1e76-4c29-9fef-454cedb6301a/images/5879781b-51f6-4a8c-9a9e-a9d09e45abf6.png)

Motion Sensors-: These are used to measure acceleration forces and rotational forces along with three axes.

Position Sensors-: These are used to measure the physical position of device.

Environmental Sensors-: These are used to measure the environmental changes such as temperature, humidity etc.

##### *In this article I am going to create sample of sensor which shows sensor ability on our device*

##### *\*1. Create an android project

##### \
2. Add views in activity_main.xml file

```
<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:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >    <ScrollView      android:id="@+id/scrollView1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:layout_alignParentTop="true"      android:layout_marginLeft="16dp"      android:layout_marginTop="16dp" >    <LinearLayout      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical" >    <TextView      android:id="@+id/textView1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Medium Text"      android:textAppearance="?android:attr/textAppearanceMedium" />       </LinearLayout>   </ScrollView> </RelativeLayout>
```

3. My string resource

```
<?xml version="1.0" encoding="utf-8"?><resources>     <string name="app_name">AndroidSensorSample</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>      <string name="list">List of sensors supported</string>       </resources></RelativeLayout>
```

4. Add following code in MainActivity.class

```
package com.example.androidsensorsample; import java.util.List; import android.hardware.Sensor;import android.hardware.SensorManager;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView; public class MainActivity extends Activity {                // SensorManager lets you access the device's sensors                private SensorManager sMgr;                List list;                @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                                TextView sensorsData = (TextView) findViewById(R.id.textView1);                                sMgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);                                list = sMgr.getSensorList(Sensor.TYPE_ALL);                                StringBuilder data = new StringBuilder();                                for (int i = 0; i < list.size(); i++) {                                                data.append(((Sensor) list.get(i)).getName() + "\n");                                                data.append(((Sensor) list.get(i)).getVendor() + "\n");                                                data.append(((Sensor) list.get(i)).getVersion() + "\n");                                }                                sensorsData.setText(data);                }                @Override                public boolean onCreateOptionsMenu(Menu menu) {                                // Inflate the menu; this adds items to the action bar if it is present.                                getMenuInflater().inflate(R.menu.main, menu);                                return true;                }}
```

Now run your application

![Sensor in Android](https://www.mindstick.com/mindstickarticle/48327284-1e76-4c29-9fef-454cedb6301a/images/153b788e-e270-4168-9d49-80a60b860ec0.png)

---

Original Source: https://www.mindstick.com/articles/1647/sensor-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
