39

Suppose I request a permission at runtime like the following:

ActivityCompat.requestPermissions(
    thisActivity,
    new String[]{Manifest.permission.READ_CONTACTS, MY_PERMISSIONS_REQUEST_READ_CONTACTS}
);

The Android system creates a popup dialog to request permission. How can I define a custom layout for that dialog?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
nullpointer
  • 533
  • 1
  • 5
  • 9

3 Answers3

66

The short answer is: you can't.

As the Android documentation puts it:

When your app calls requestPermissions(), the system shows a standard dialog box to the user. Your app cannot configure or alter that dialog box. If you need to provide any information or explanation to the user, you should do that before you call requestPermissions(), as described in "Explain why the app needs permissions".

In summary: There is no way to define a custom layout for the permission dialog for now.

You can find more detailed information here: http://developer.android.com/training/permissions/requesting.html

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • 1
    "android... allows you to add some text to that dialog explaining the user why you are requesting that permission" -- not that I am aware of. – CommonsWare Oct 21 '15 at 18:11
  • @CommonsWare i thought the developer could explain the rationale of using that permission is that wrong? or just android shows its default explanation about that permission? – yrazlik Oct 21 '15 at 18:13
  • 3
    You can find out, via `shouldShowPermissionRequestRationale()`, whether you might want to show an explanation. However, you have to do that in your own UI. You cannot add prose to the system dialog for that. – CommonsWare Oct 21 '15 at 18:15
22

I have create custom dialog for permission:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
   if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
       alertBuilder.setCancelable(true);
       alertBuilder.setTitle("Permission necessary");
       alertBuilder.setMessage("External storage permission is necessary");
       alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

       @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
       public void onClick(DialogInterface dialog, int which) {
           ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);}});

       AlertDialog alert = alertBuilder.create();
       alert.show();
   } else { 
          ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);}
    return false;} 
 else { 
     return true;
}

Hope this will help you :)

Anand Savjani
  • 2,484
  • 3
  • 26
  • 39
  • 2
    Looks like this just create a new dialogue that when clicked yes, will trigger the system permission dialogue, it does not change the actual permission dialog in Android. No different than calling ActivityCompat.requestPermissions... directly – BabyishTank Sep 28 '20 at 16:38
  • @BabyishTank : You can not customize system dialog. It's just to create a customize dialog with your own custom message. You can directly call with system permission dialogue. – Anand Savjani Sep 30 '20 at 15:08
  • What if I have a custom permission, not a system standard one? – IgorGanapolsky Sep 28 '22 at 17:57
0

Android 6.0 Marshmallow. Cannot write to SD Card this post helped me out from showing dialog to grant write to external permission

Community
  • 1
  • 1
Bharath
  • 277
  • 4
  • 10