Android – Proximity Sensor using Kotlin
Proximity Sensor is one of the sensors in mobile devices we use almost daily. This sensor is present in the top section of your phone. The sensor is used to detect the presence of any object in the proximity of the phone. This sensor is used in many calling apps when the user keeps his phone on his ears for listening then the screen automatically turns off so to avoid unnecessary screen touch events while on call. In this article, we will look at How to use Proximity Sensor in android using Kotlin.
Note: If you are looking to implement the Proximity Sensor in your android application using Java. Check out the following article: How to Use Proximity Sensor in Android using Java
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 permissions to use the proximity sensor in our Android App
Navigate to app>manifest>AndroidManifest.xml and add the below permission to it in the manifest section.
XML
< uses-permission android:name = "android.hardware.sensor.proximity" /> |
Step 3: 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. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a text for heading of our app--> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:gravity = "center" android:padding = "4dp" android:text = "QR Code Generator" android:textAlignment = "center" android:textColor = "@color/purple_200" android:textSize = "18sp" android:textStyle = "bold" /> <!--Text view to display sensor status--> < TextView android:id = "@+id/idTVSensorStatus" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:gravity = "center" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "50dp" /> </ RelativeLayout > |
Step 4: Working with MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject import android.content.Context import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager import android.os.Bundle import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating // a variable for our text view. lateinit var sensorStatusTV: TextView // on below line we are creating // a variable for our proximity sensor lateinit var proximitySensor: Sensor // on below line we are creating // a variable for sensor manager lateinit var sensorManager: SensorManager override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing our all variables. sensorStatusTV = findViewById(R.id.idTVSensorStatus) // on below line we are initializing our sensor manager sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager // on below line we are initializing our proximity sensor variable proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) // on below line we are checking if the proximity sensor is null if (proximitySensor == null ) { // on below line we are displaying a toast if no sensor is available Toast.makeText( this , "No proximity sensor found in device.." , Toast.LENGTH_SHORT).show() finish() } else { // on below line we are registering // our sensor with sensor manager sensorManager.registerListener( proximitySensorEventListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL ) } } // calling the sensor event class to detect // the change in data when sensor starts working. var proximitySensorEventListener: SensorEventListener? = object : SensorEventListener { override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) { // method to check accuracy changed in sensor. } override fun onSensorChanged(event: SensorEvent) { // check if the sensor type is proximity sensor. if (event.sensor.type == Sensor.TYPE_PROXIMITY) { if (event.values[ 0 ] == 0f) { // here we are setting our status to our textview.. // if sensor event return 0 then object is closed // to sensor else object is away from sensor. sensorStatusTV.text = "Object is Near to sensor" } else { // on below line we are setting text for text view // as object is away from sensor. sensorStatusTV.text = "Object is Away from sensor" } } } } } |
Now run your application to see the output of it.
Output:
Contact Us