Open In App

Android Runtime Permissions with Dexter using Android Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

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 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 in the Color.kt file

Navigate to app>java>your app’s package name>ui.theme>Color.kt file and add below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.example.newcanaryproject.ui.theme
 
import androidx.compose.ui.graphics.Color
 
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
 
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 3: Adding permission in AndroidManifest.xml

Navigate to app > manifest > AndroidManifest.xml and add the below permissions in the manifest tag. 

XML




<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Step 4: Adding dependency for using Dexter in build.gradle

Navigate to Gradle Scripts>build.gradle and add the below dependency in the dependencies section. 

implementation 'com.karumi:dexter:6.2.2'

After adding this dependency simply sync your project to install it. 

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. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
 
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.material.*
import androidx.compose.runtime.*
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.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.PermissionToken
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
 
class MainActivity : ComponentActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying
                // background color for our application
                Surface(
                    // on below line we are specifying
                    // modifier and color for our app
                    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 = "Dexter in Android",
 
                                        // 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.
                                        color = Color.White
                                    )
                                }
                            )
                        }
                    ) {
                        // on below line we are calling pdf generator
                        // method for generating a pdf file.
                        dexterPermissions()
                    }
                }
            }
        }
    }
}
 
// on below line we are creating a
// dexter permissions function for ui.
@Composable
fun dexterPermissions() {
 
    // on below line we are creating a variable for
    // our context and activity and initializing it.
    val ctx = LocalContext.current
 
    // on below line we are creating a column for our ui.
    Column(
        // in this column we are adding a modifier for our
        // column and specifying max width, height and size.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize()
 
            // on below line we are adding padding
            // from all sides to our column.
            .padding(6.dp),
 
        // on below line we are adding vertical
        // arrangement for our column as center
        verticalArrangement = Arrangement.Center,
 
        // on below line we are adding
        // horizontal alignment for our column.
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
 
        // on below line we are creating a
        // simple text as a PDF Generator.
        Text(
            // on below line we are
            // setting text to our text
            text = "Dexter in Android",
 
            // on below line we are
            // setting color for our text
            color = greenColor,
 
            // on below line we are setting
            // font weight for our text
            fontWeight = FontWeight.Bold,
 
            // on below line we are setting
            // alignment for our text as center.
            textAlign = TextAlign.Center,
 
            // on below line we are setting
            // font size for our text
            fontSize = 20.sp
        )
 
        // on below line we are adding spacer
        // between text and a button.
        Spacer(modifier = Modifier.height(60.dp))
 
        // on below line we are creating a button.
        Button(
            // on below line we are adding a modifier to
            // it and specifying max width to it.
            modifier = Modifier
                .fillMaxWidth()
 
                // on below line we are adding padding for our button.
                .padding(20.dp),
 
            // on below line we are adding on click for our button.
            onClick = {
 
                // inside on click we are calling get permissions
                // method to request permissions.
                getPermission(ctx)
            }) {
 
            // on below line we are displaying a text for our button.
            Text(modifier = Modifier.padding(6.dp), text = "Request Permission")
        }
    }
 
}
 
// on below line we are creating a get
// permission method to get all permissions.
fun getPermission(context: Context) {
    // on below line we are creating a variable
    // for dexter and initializing it.
    val dexter = Dexter.withContext(context)
        .withPermissions(
 
            // on below line we are specifying the permissions
            // which we have to request from our user.
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.RECORD_AUDIO
 
        ).
        // on below line we are adding listener for permissions.
        withListener(object : MultiplePermissionsListener {
 
            // inside this on below line we are calling
            // a method on permission check to check the permissions.
            override fun onPermissionsChecked(report: MultiplePermissionsReport) {
 
                // on below line we are checking the status of permissions.
                report.let {
 
                    // on below line we are checking if all the permissions are granted.
                    if (report.areAllPermissionsGranted()) {
                        // if all the permissions are granted we are displaying
                        // a simple toast message.
                        Toast.makeText(context, "Permissions Granted..", Toast.LENGTH_SHORT).show()
                    } else {
 
                        // if the permissions are not accepted we are displaying
                        // a toast message as permissions denied on below line.
                        Toast.makeText(context, "Permissions Denied..", Toast.LENGTH_SHORT).show()
                    }
                }
            }
 
            // on below line we are calling on permission
            // rational should be shown method.
            override fun onPermissionRationaleShouldBeShown(
                p0: MutableList<com.karumi.dexter.listener.PermissionRequest>?,
                token: PermissionToken?
            ) {
                // in this method we are calling continue
                // permission request until permissions are not granted.
                token?.continuePermissionRequest()
            }
        }).withErrorListener {
 
            // on below line method will be called when dexter
            // throws any error while requesting permissions.
            Toast.makeText(context, it.name, Toast.LENGTH_SHORT).show()
        }
    dexter.check()
}


Now run your application to see the output of it. 

Output: 



Last Updated : 20 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads