Easy Runtime Permissions in Android with Dexter
The runtime permission in Android which introduced in Marshmallow and due to these runtime permissions user can grant permission to the app in runtime, also while writing the code for runtime permissions. Sometimes handling the task of requesting permissions become so difficult and the developer has to write a huge amount of code. So we will see towards the implementation of Runtime permissions in Android using Dexter.
What is Dexter in Android?
Dexter is the library that will help us to make this task easy for handling runtime permissions in Android. Now we will see the implementation of this in our Android app.
What we are going to build in this article?
We will be building a simple application in which we will be showing a button to the user. After clicking on that button we will display runtime permissions to the user. So if the users deny the permission then we are showing the option to open the settings screen and grant the permissions. Below is the short video in which we will get to see what we are going to build in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency of Dexter runtime permissions in build.gradle file
Navigate to gradle scripts and then to build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.
After adding this dependency now sync your project. Let’s move towards the XML part.
Step 3: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 5: Adding the required permissions in the Manifest file
Navigate to the app > AndroidManifest.xml file and add the below permissions to it.
After adding these permissions in our AndroidManifest.xml file. Now run our app and see the output of the code.