Skip to main content

Customize Run Time Permission Dialog in Android


Hello. We all know how run time permissions came into effect from Android 6.0 ( Marshmallow ). The advantage is that it's more user friendly and it gives the user an idea about why he has to give such permissions for the app. Such as giving his contact details , his location details etc. But, even-though there is one flaw in it. Maybe exactly not a flaw, but we can't really customize that run-time permission dialog or it's contents. Android OS won't allow us to customize it's default settings in this case. So, the only option now remaining is to pop up a dialog just before the original run-time permission dialog. You can then describe to your user what exactly you are going to do by accessing such permissions. I have found many answers on StackOverflow and I combined those answers to my required one.
What we are going to do today is asking the permission of user for accessing his location. Simply said, asking him to turn on his GPS.
There is a method called checkPermission(String, String) in android which helps to determine whether you have been granted a particular permission. We can utilize this method in-order to check every time a particular permission has been granted by the user. Add this line inside your onCreate method.
 result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);  
Now call a method called requestPermission() . This method will prompt every single time to ask your user to grant the permission, it'll also pops up even if the user denied the permission the first time
 private void requestPermission() {  
     if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,  
         Manifest.permission.ACCESS_FINE_LOCATION)) {  
       if (result != PackageManager.PERMISSION_GRANTED) {  
         AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);  
         alertBuilder.setCancelable(true);  
         alertBuilder.setTitle("Location permission necessary");  
         alertBuilder.setMessage(MainActivity.this.getString(R.string.location_permission_txt));  
         alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialog, int which) {  
             ActivityCompat.requestPermissions(MainActivity.this,  
                 new String[]{Manifest.permission.ACCESS_FINE_LOCATION},  
                 PERMISSION_REQUEST_CODE);  
           }  
         });  
         AlertDialog alert = alertBuilder.create();  
         alert.show();  
       }  
     } else  
     {  
 //      int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);  
       if (result != PackageManager.PERMISSION_GRANTED) {  
         AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);  
         alertBuilder.setCancelable(true);  
         alertBuilder.setTitle("Location permission necessary");  
         alertBuilder.setMessage(MainActivity.this.getString(R.string.location_permission_txt));  
         alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialog, int which) {  
             ActivityCompat.requestPermissions(MainActivity.this,  
                 new String[]{Manifest.permission.ACCESS_FINE_LOCATION},  
                 PERMISSION_REQUEST_CODE);  
           }  
         });  
         AlertDialog alert = alertBuilder.create();  
         alert.show();  
       }  
     }  
   }  
This will help you to tell your user exactly why you need this permission granted. App source code is available on GitHub :- https://github.com/clint22/CustomizeRunTimePermissionDialog

ScreenShots

Comments

Popular posts from this blog

Barcode Scanner in Android using zxing Library

Hi Everyone, Last week I tried to implement a barcode scanner using the zxing library. Writing this blog on this topic because I found the tutorials about barcode scanner using zxing is either pretty much older or confusing. Let's start by importing these two libraries in our Android project. compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar' compile 'com.google.zxing:core:3.2.0' Practically , we only use the back camera for scanning the barcode because it has more clarity and auto-focus feature. But , I will also show how to implement the same with the front camera. Create a new layout and add two buttons for opening the barcode scanner with front camera and back camera. activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http

Expandable ListView in Android on Complex Layout

Hello, Last day while at work I encountered an issue such as when I try to implement a expandable listview the override method "@getChildView" is not being called. When I looked around, I found out that the issue is mainly related to the expandable listview being inside a scrollview or any such complex layouts. I fixed the issue after searching through some answers on stack overflow, I have provided the links of the original answer at the end of the post Response will be of the format { "movie_details": [ { "name": "Fight Club", "type": "Movie", "id": 1 }, { "name": "13 reasons why", "type": "Series", "id": 2 }, { "name": "Dunkirk", "type": "Movie", "id": 3 }, { "name": "Game of thrones", &