Display List of Sensors in Android using Jetpack Compose
Last Updated :
31 Aug, 2022
Many sensors are present within mobile devices that are used for different purposes for performing various functions within the android device and sensing the data. In this article, we will be building a simple application to display the list of available sensors within our android applications using Jetpack Compose.
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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Adding a new color to the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
package com.example.testproject.ui.theme
import androidx.compose.ui.graphics.Color
val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// on below line we are adding different colors
val greenColor = Color(0xFF0F9D58)
Step 3: 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. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.example.testproject
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.testproject.ui.theme.TestProjectTheme
import com.example.testproject.ui.theme.greenColor
class MainActivity : ComponentActivity() {
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestProjectTheme {
// on below line we are specifying
// background color for our application
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
// on below line we are specifying theme as scaffold.
Scaffold(
// in scaffold we are specifying top bar.
topBar = {
// inside top bar we are specifying background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying
// title for our top bar.
title = {
// in the top bar we are specifying
// tile as a text
Text(
// on below line we are specifying
// text to display in top app bar.
text = "GFG",
color = Color.White,
// on below line we are specifying
// modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// on below line we are
// specifying text alignment.
textAlign = TextAlign.Center,
// on below line we are
// specifying color for our text.
)
}
)
}
) {
// on below line we are calling proximity
// sensor method to use proximity sensor.
displaySensors()
}
}
}
}
}
}
@Composable
fun displaySensors() {
// on below line we are creating
// a variable for a context
val ctx = LocalContext.current
// on below line we are creating a column
Column(
// on below line we are specifying modifier
// and setting max height and max width
// for our column
modifier = Modifier
.fillMaxSize()
.fillMaxHeight()
.fillMaxWidth()
// on below line we are
// adding padding for our column
.padding(5.dp),
// on below line we are specifying horizontal
// and vertical alignment for our column
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// on below line we are initializing our sensor manager.
var sensorManager: SensorManager =
ctx.getSystemService(Context.SENSOR_SERVICE) as SensorManager
// on below line we are creating list for device sensors.
var deviceSensors: List<Sensor> = sensorManager.getSensorList(Sensor.TYPE_ALL)
// on below line we are creating a simple text
// in which we are displaying a text as Object is
Text(
text = "Sensors in Devices are : ",
// on below line we are setting text color
color = Color.Black,
// on below line we are specifying font weight
fontWeight = FontWeight.Bold,
// on below line we are specifying font family.
fontFamily = FontFamily.Default,
// on below line we are specifying
// font size and padding from all sides.
fontSize = 20.sp, modifier = Modifier.padding(5.dp)
)
// on below line creating a variable for sensor data.
var sensorsData = ""
// on below line adding all sensors from
// device sensors in our variable.
for (sens in deviceSensors) {
sensorsData = sensorsData + sens.name + " \n\n"
}
// on below line we are creating a simple text
// in which we are displaying a text as all
// sensors in device
Text(
text = sensorsData,
// on below line we are setting text color
color = Color.Black,
// on below line we are specifying font weight
fontWeight = FontWeight.Black,
// on below line we are specifying font family.
fontFamily = FontFamily.Default,
// on below line adding text alignment.
textAlign = TextAlign.Center,
// on below line we are specifying
// font size and padding from all sides.
fontSize = 12.sp,
modifier = Modifier.padding(5.dp)
)
}
}
Now run your application to see the output of it.
Output:
Similar Reads
ListView in Android using Jetpack Compose A ListView is a UI component that displays data in a vertically scrollable list, where each item is placed one below the other. It is widely used in Android applications to showcase categorized data in an organized manner. In Jetpack Compose, the traditional ListView from XML-based layouts is replac
2 min read
Date Picker in Android using Jetpack Compose In Android, a Date Picker is a widget used to select a date from the calendar. When a Date Picker is implemented in an application, the user can select a year, a month, and a day, combining to form date from a calendar-like-looking box. This data is generally needed and collected in applications tha
2 min read
Draw a Line in Android using Jetpack Compose In Android, a Canvas is used when we need to draw objects of different shapes and types. Canvas is a class in Android that performs 2D drawings of different objects onto the screen. It can also be used for drawing a straight line between two given points. So in this article, we will show you how you
2 min read
Proximity Sensor in Android App using Jetpack Compose Proximity Sensor is one of the sensors present in the mobile device which is used by everyone using smartphone. This sensor is present in mobile devices under the earpiece section. This sensor is used within the mobile device when the user is attending a call to prevent the disconnection of the call
6 min read
Custom ListView in Android using Jetpack Compose Listview is used to display data in a vertically scrollable manner. We have seen How to create a simple ListView in Android Jetpack Compose. In this article, we will take a look at How to create a Custom ListView for displaying multiple data within it using Jetpack Compose. We will be creating a sim
3 min read
Services in Android using Jetpack Compose Services in Android applications are used to perform some background tasks within android applications. We can call the service to perform a specific task in the background of our application. That task will be performed continuously if our application is not visible to the user. Services are genera
6 min read
Horizontal ListView in Android using Jetpack Compose Many Android Applications present data in the form of horizontally scrollable lists. This list can be scrolled horizontally. In this article, we will take a look at How to Implement Horizontal ListView in Android Applications using Jetpack Compose. Note: If you are seeking Java code for Jetpack Com
6 min read
Text in Android using Jetpack Compose Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. Android Text is a simple view in Android which is used to display text inside our App. In this article, we will take a look at the implementation of Text in Android using Jetpack Compose. Importan
5 min read
SmsManager in Android using Jetpack Compose Many times while building an android application we have to add a feature through which users will be able to directly send SMS from our android application. So in this article, we will take a look at How to send a text message over a phone using SMS Manager in Android using Jetpack Compose. A sampl
6 min read
How to Keep Device Screen On in Android using Jetpack Compose? Many times while building an android application we have to make sure that the user's device screen remains on. In the video player application, we can see that the user's device screen remains on until the video is not completed. For this, we have to keep the device screen on. In this article, we w
4 min read