Android Easy Runtime Permissions With Dexter Library Example Tutorial

Today’s topic is about easy runtime permissions with dexter library example tutorial.

Asking for runtime permissions to user is a little bit of complex task to handle.

Developer need to take care of user experience while implementing feature carefully because user does not like to deal with dialogs very often while using the app.

Also, developer have to write plenty of coding lines for getting single permission. So, think about scenario where multiple permissions are required.

Dexter is a github third party library which simplifies the process of requesting runtime permissions.

Dexter have predefined classes which can work with very few lines of coding. You can ask for single as well as multiple permissions using dexter with only one method.

We will see how to request permissions with simple technique and in easy manner with example.

First of all, check the final output of this tutorial by watching the below video.

Step 1. Integrating Dexter Library

You need to add the following line your build.gradle(Module: app) file.

Above line will fetch all the necessary classes from the github dexter library.

So that we can use them with less number of coding lines.

Step 2. Android Manifest

Now simply add required permissions in your AndroidManifext.xml file.

In this tutorial, I will add below five permissions.

You can change them as per your app’s requirements.

Step 3. Creating Main Layout

Now let us create a main layout of our app.

Write down the following coding lines in the activity_main.xml file

  • Here, I have added three buttons.
  • One button will ask for single permission (only Camera permission).
  • Second button will ask for multiple permissions (Contact, Storage and Audio Permissions).
  • Third button will also ask for single permission, but we will show a snackbar when the user denies the permission in this scenario.

Step 4. Java Coding For Main Activity

At last, write down the following code in the file

Above is the main heart of the whole tutorial example.

Let us fully read and understand what above code is exactly doing.

Consider the below code

  1. I have declared three buttons and one object of the PermissionListener class from the dexter library.

Below is the declaration of the view object

  • Above line is defining a view which contains the main layout. We have used this view object (parentLayout) in the declaration of the snackbarPermissionListener.

Following is the declaration of the snackbarPermissionListener.

  • We will use this snackbarPermissionListener to pop up the snack bar when the user deny any permissions.
  • We have used this snackbar to tell user about the importance of granting the required permission.

Now let us see the button clicks.

When the user clicks on the Camera button, compiler will execute the above code.

Asking for Single Permission

  • It will call requestSinglePermission() method.

Below is the coding for requestSinglePermission() method.

  • As you can see in the above code, Dexter.withActivity is used to ask the camera permission.

You need to pass the permission in below line

  • When the user grants the permission, onPermissionGranted() method is called.
  • You can write the code of the feature which uses this permission in this method.

When the user denies the permission, compiler will call the following code

  • I will open a new dialog when the user denies the permission. This dialog is opened by using the openSettingsDialog() method.

Code for openSettingsDialog() method is as following

  • builder.setMessage() will contain a message that tell user why we need runtime permission.

Following code will open app settings when the user clicks on “Take Me To Settings” button.

Asking for Multiple Permissions

  • When the user clicks on the Contact,Storage,Audio button, compiler will call the requestMultiplePermissions() method.

Following is the source code snippet for requestMultiplePermissions() method.

  • You need to pass the multiple permissions in the below line

  • After that, compiler will call a method onPermissionsChecked().
  • In onPermissionsChecked() method, compiler will execute following lines if use have granted all the permissions.

  • If the user have not granted all the permissions, system will run the below source code

  • openSettingsDialog() is the same method as we have created in the requestSinglePermission() method.

So it was all lines about this android runtime permissions with dexter tutorial example.

Feel free to ask any query in the comment section.

Also Read,

Download the Source Code for Runtime Permissions With Dexter

Click the below link to download whole android studio source code

[sociallocker]Download Android Runtime Permission Dexter Example[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *