How to Build a Sensor App in Android?
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 android:layout_width = "match_parent" android:layout_height = "match_parent" 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 } } } } } } |