Android applications require the usage of hardware devices within the android applications such as microphones or cameras. For using these devices within any android application. Permissions for using this hardware have to be provided to the application. For giving these permissions to the application runtime permissions are used. Handling permissions manually for the different tasks is a complicated process. To handle this situation we can use the Dexter library for requesting permissions within our application.
In this article, we will be creating a simple application to request permissions using Dexter in android using Jetpack Compose.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.
Step 2: Adding permission in AndroidManifest.xml
Navigate to app > manifest > AndroidManifest.xml and add the below permissions in the manifest tag.
AndroidManifest.xml:
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Step 3: Adding dependency for Dexter in gradle file
Navigate to Gradle Scripts > build.gradle.kts (Module :app) and add the below dependency in the dependencies section.
dependencies {
...
implementation ("com.karumi:dexter:6.2.2")
}
After adding this dependency simply sync your project to install it.
Step 4: 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.
MainActivity.kt:
package com.geeksforgeeks.demo
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.*
import com.karumi.dexter.*
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
DexterPermissions()
}
}
}
}
@Composable
fun DexterPermissions() {
// initialize context
val context = LocalContext.current
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.fillMaxSize()
.padding(6.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// a button to request permissions
Button(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
onClick = {
// call function to request permissions
getPermission(context)
}) {
// text inside button
Text(modifier = Modifier.padding(6.dp), text = "Request Permission")
}
}
}
// function to request for permissions
fun getPermission(context: Context) {
// initializing dexter with context
val dexter = Dexter.withContext(context)
// list all permissions required
.withPermissions(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.CAMERA,
android.Manifest.permission.RECORD_AUDIO
)
// add listener for permissions
.withListener(object : MultiplePermissionsListener {
// call a method on permission check to check the permissions
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
// check the status of permissions
report.let {
// check if all the permissions are granted
if (report.areAllPermissionsGranted()) {
Toast.makeText(context, "Permissions Granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Permissions Denied", Toast.LENGTH_SHORT).show()
}
}
}
// call on permission rational should be shown method
override fun onPermissionRationaleShouldBeShown(
p0: MutableList<com.karumi.dexter.listener.PermissionRequest>?,
token: PermissionToken?
) {
// call continue permission request until permissions are not granted
token?.continuePermissionRequest()
}
}).withErrorListener {
// handle errors
Toast.makeText(context, it.name, Toast.LENGTH_SHORT).show()
}
dexter.check()
}