Dexter Library is used to easily access runtime permissions. As Marshmallow has provided facility to grant permissions but still for granting each and every permission, the developer needs to write long and tedious codes. So, to overcome this, we use Dexter Library.

Below is an Easy Implementation of Dexter Library:

1. Add Dexter Permissions Library

(build.gradle(Module: app)

implementation 'com.karumi:dexter:5.0.0'

2. Add Permissions in Android Manifest File

< uses-permission android:name="android.permission.CAMERA" / > < uses-permission android:name="android.permission.READ_CONTACTS" / > < uses-permission android:name="android.permission.RECORD_AUDIO" / > < uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> < uses-permission android:name="android.permission.CALL_PHONE" />

3. Create XML File for Layout

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="DEXTER LIBRARY DEMO" android:textAlignment="center" android:textSize="20dp" android:textColor="#ffffff" android:background="#000000"/> <Button android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="single permission" android:layout_marginTop="50dp" android:onClick="func1"/> // func1() is called when we click on “SINGLE PERMISSION” button. <Button android:id="@+id/b2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="multiple permissions" android:onClick="multiplepermissions"/>// multiplepermissions() is called when we click on “MULTIPLE PERMISSIONS” button </LinearLayout>

Read More: How to change the background transition in Android Studio

4. Create Java File (MAIN ACTIVITY.java)

package com.example.dexterlibrary; import android.Manifest; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.provider.Settings; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.karumi.dexter.Dexter; import com.karumi.dexter.MultiplePermissionsReport; import com.karumi.dexter.PermissionToken; import com.karumi.dexter.listener.DexterError; import com.karumi.dexter.listener.PermissionDeniedResponse; import com.karumi.dexter.listener.PermissionGrantedResponse; import com.karumi.dexter.listener.PermissionRequest; import com.karumi.dexter.listener.PermissionRequestErrorListener; import com.karumi.dexter.listener.multi.MultiplePermissionsListener; import com.karumi.dexter.listener.single.PermissionListener; import java.util.List;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }public void func1(View view) { singlePerm(); } public void singlePerm() { Dexter.withActivity(this) .withPermission(Manifest.permission.CAMERA) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) { //Single Permission is granted Toast.makeText(MainActivity.this, "Enable single permission", Toast.LENGTH_SHORT).show(); } @Override public void onPermissionDenied(PermissionDeniedResponse response) { // check for permanent denial of permission if (response.isPermanentlyDenied()) { openSettingsDialog(); } } @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) { token.continuePermissionRequest(); } }).check(); } public void openSettingsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Permissions Required"); builder.setMessage("Permission is required for using this app. Please enable them in app settings."); builder.setPositiveButton("Go to SETTINGS", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); showsettings(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } public void showsettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, 101); } public void multiplepermissions(View view) { Dexter.withActivity(this) .withPermissions( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CONTACTS, Manifest.permission.RECORD_AUDIO, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA) .withListener(new MultiplePermissionsListener() { @Override public void onPermissionsChecked(MultiplePermissionsReport report) { // if all permissions are allowed if (report.areAllPermissionsGranted()) { Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show(); } // if permanent denial of any permission if (report.isAnyPermissionPermanentlyDenied()) { openSettingsDialog();// show alert dialog box } } @Override public void onPermissionRationaleShouldBeShown(List permissions, PermissionToken token) { token.continuePermissionRequest(); } }). withErrorListener(new PermissionRequestErrorListener() { @Override public void onError(DexterError error) { Toast.makeText(getApplicationContext(), "Error!! ", Toast.LENGTH_SHORT).show(); } }) .onSameThread() .check(); } }

4.1 Granting Single Permission

onPermissionGranted() will be called when the permission is granted.

will be called when the permission is granted. onPermissionDenied() will be called when the permission is rejected.

public void func1(View view) { singlePerm(); } public void singlePerm() { Dexter.withActivity(this) .withPermission(Manifest.permission.CAMERA) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) { //Single Permission is granted Toast.makeText(MainActivity.this, "Enable single permission", Toast.LENGTH_SHORT).show(); } @Override public void onPermissionDenied(PermissionDeniedResponse response) { // check for permanent denial of permission if (response.isPermanentlyDenied()) { openSettingsDialog(); } } @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) { token.continuePermissionRequest(); } }).check(); } public void openSettingsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Permissions Required"); builder.setMessage("Permission is required for using this app. Please enable them in app settings."); builder.setPositiveButton("Go to SETTINGS", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); showsettings(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } public void showsettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, 101); }

4.2 Granting Multiple Permissions

If you to multiple permission then, you can use withPermissions() method.

public void multiplepermissions(View view) { Dexter.withActivity(this) .withPermissions( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CONTACTS, Manifest.permission.RECORD_AUDIO, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA) .withListener(new MultiplePermissionsListener() { @Override public void onPermissionsChecked(MultiplePermissionsReport report) { // if all permissions are allowed if (report.areAllPermissionsGranted()) { Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show(); } // if permanent denial of any permission if (report.isAnyPermissionPermanentlyDenied()) { openSettingsDialog();// show alert dialog box } } @Override public void onPermissionRationaleShouldBeShown(List permissions, PermissionToken token) { token.continuePermissionRequest(); } }). withErrorListener(new PermissionRequestErrorListener() { @Override public void onError(DexterError error) { Toast.makeText(getApplicationContext(), "Error!! ", Toast.LENGTH_SHORT).show(); } }) .onSameThread() .check(); } public void openSettingsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Permissions Required"); builder.setMessage("Permission is required for using this app. Please enable them in app settings."); builder.setPositiveButton("Go to SETTINGS", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); showsettings(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } public void showsettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, 101); }