How to Build a Sensor App in Android?
Last Updated :
26 Apr, 2025
Android mobile phones have sensors, so we can perform various functions like Controlling screen brightness, Monitoring acceleration along a single axis, Motion detection, etc. In this article, we will be building an application that will determine the intensity of light in the room with the help of light sensors. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add permission to access sensors
Add this line inside the AndroidManifest.xml file add this line
XML
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
Step 3: Add Circular progress bar dependency inside build.gradle(app) file
Kotlin
implementation 'com.mikhaellopez:circularprogressbar:3.1.0'
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We are using the Circular ProgressBar in this project which will change its color when the intensity of light changes.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#000"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/progress_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="100dp">
<com.mikhaellopez.circularprogressbar.CircularProgressBar
android:id="@+id/circularProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cpb_background_progressbar_color="#b6bbd8"
app:cpb_background_progressbar_width="5dp"
app:cpb_progress_direction="to_right"
app:cpb_progressbar_color="#3f51b5"
app:cpb_progressbar_width="10dp"
app:cpb_round_border="false" />
<!--Text implementation in center
of the progress bar-->
<TextView
android:id="@+id/intensity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#ffff"
android:textSize="28sp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
</TextView>
<TextView
android:id="@+id/light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textAlignment="center"
android:textColor="#ffff">
</TextView>
</LinearLayout>
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. As the last step, we will be writing Kotlin code inside MainActivity.kt file. We can use the if else condition and display the message inside the text view according to the intensity of light in the room. Similarly, we can change the color of the ProgressBar according to the intensity of light.
Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mySensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
val lightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
if (lightSensor != null) {
available!!.text = "LIGHT Available"
mySensorManager.registerListener(
lightSensorListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL
)
} else {
available!!.text = "LIGHT NOT Available"
}
}
private val lightSensorListener: SensorEventListener = object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {
// TODO Auto-generated method stub
}
override fun onSensorChanged(event: SensorEvent) {
if (event.sensor.type == Sensor.TYPE_LIGHT) {
intensity!!.text = "" + event.values[0]
if (event.values[0] in 0.0..50.0) {
light!!.text = "Switch On Lights"
circularProgressBar.apply {
// Set Progress
progress = 65f
// or with animation
setProgressWithAnimation(65f, 1000) // =1s
// Set Progress Max
progressMax = 200f
// Set ProgressBar Color
progressBarColor = Color.BLACK
// or with gradient
progressBarColorStart = Color.GRAY
progressBarColorEnd = Color.WHITE
progressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set background ProgressBar Color
backgroundProgressBarColor = Color.GRAY
// or with gradient
backgroundProgressBarColorStart = Color.WHITE
backgroundProgressBarColorEnd = Color.WHITE
backgroundProgressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set Width
progressBarWidth = 7f // in DP
backgroundProgressBarWidth = 3f // in DP
// Other
roundBorder = true
startAngle = 180f
progressDirection = CircularProgressBar.ProgressDirection.TO_RIGHT
}
} else if (event.values[0] in 50.0..100.0) {
light!!.text = "Dim Light"
circularProgressBar.apply {
// Set Progress
progress = 65f
// or with animation
setProgressWithAnimation(65f, 1000) // =1s
// Set Progress Max
progressMax = 200f
// Set ProgressBar Color
progressBarColor = Color.BLACK
// or with gradient
progressBarColorStart = Color.GRAY
progressBarColorEnd = Color.RED
progressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set background ProgressBar Color
backgroundProgressBarColor = Color.GRAY
// or with gradient
backgroundProgressBarColorStart = Color.WHITE
backgroundProgressBarColorEnd = Color.WHITE
backgroundProgressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set Width
progressBarWidth = 7f // in DP
backgroundProgressBarWidth = 3f // in DP
// Other
roundBorder = true
startAngle = 180f
progressDirection = CircularProgressBar.ProgressDirection.TO_RIGHT
}
} else if (event.values[0] in 100.0..200.0) {
light!!.text = "Bright Light"
circularProgressBar.apply {
// Set Progress
progress = 65f
// or with animation
setProgressWithAnimation(65f, 20000) // =2s
// Set Progress Max
progressMax = 200f
// Set ProgressBar Color
progressBarColor = Color.BLACK
// or with gradient
progressBarColorStart = Color.GRAY
progressBarColorEnd = Color.RED
progressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set background ProgressBar Color
backgroundProgressBarColor = Color.GRAY
// or with gradient
backgroundProgressBarColorStart = Color.WHITE
backgroundProgressBarColorEnd = Color.RED
backgroundProgressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set Width
progressBarWidth = 7f // in DP
backgroundProgressBarWidth = 3f // in DP
// Other
roundBorder = true
startAngle = 180f
progressDirection = CircularProgressBar.ProgressDirection.TO_RIGHT
}
} else {
circularProgressBar.apply {
light!!.text = "Very Bright Light"
progress = 65f
// or with animation
setProgressWithAnimation(65f, 1000) // =1s
// Set Progress Max
progressMax = 200f
// Set ProgressBar Color
progressBarColor = Color.BLACK
// or with gradient
progressBarColorStart = Color.GRAY
progressBarColorEnd = Color.RED
progressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set background ProgressBar Color
backgroundProgressBarColor = Color.GRAY
// or with gradient
backgroundProgressBarColorStart = Color.WHITE
backgroundProgressBarColorEnd = Color.MAGENTA
backgroundProgressBarColorDirection =
CircularProgressBar.GradientDirection.TOP_TO_BOTTOM
// Set Width
progressBarWidth = 7f // in DP
backgroundProgressBarWidth = 3f // in DP
// Other
roundBorder = true
startAngle = 180f
progressDirection = CircularProgressBar.ProgressDirection.TO_RIGHT
}
}
}
}
}
}
Output:
Similar Reads
How to Build a Pomodoro App in Android?
The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short breaks of 5 minutes. These intervals are known as "pomodoros". The method is based
4 min read
How to Build a Weather App in Android?
In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components
6 min read
How to Build a Simple Notes App in Android?
Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
9 min read
How to Build a Rick and Morty App in Android?
Rick and Morty is an American animated science fiction sitcom created by Justin Roiland and Dan Harmon. In this article, we will build an application that will display the name and image of all rick and Morty characters using this API. In order to build this application we will be using the Retrofit
5 min read
How to Build a Simple Alarm Setter App in Android?
In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu
5 min read
How to Create a News App in Android?
Networking is an integral part of android development, especially when building data-driven clients. The java class mainly used for network connections is HttpUrlConnection. The class requires manual management of data parsing and asynchronous execution of requests. For network operations, we are be
9 min read
How to Build a Simple Magic 8 Ball App in Android?
In this article, we will be building a Magic 8 Ball App Project using Java and XML in Android. The application is based on a decision making application. In this app, a user can ask the ball what decision they should make. The ball will randomly answer Yes, No, Not Sure, and other such answers. Ther
3 min read
How to Build an Application to Test Motion Sensors in Android?
In this article, we will be building a Motion Sensor Testing App Project using Java and XML in Android. The application will be using the hardware of the device to detect the movements. The components required to detect the motion are Accelerometer and Gyroscope. The Accelerometer is an electronic s
6 min read
How to Build Live Train Status App in Android?
Imagine you are traveling via Indian railways which is one of the biggest rail networks in the world. You want to get real-time updates for the train on which you are traveling so that you can plan accordingly. In this article, we will take a look at How to build a live train status checker applicat
8 min read
How to Build a Temperature Converter App in Android?
Temperature Converter Application is built using android studio where temperature entered in Celsius can be converted to Fahrenheit. The output will be displayed on Snackbar. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to impleme
3 min read