How can i customize permission dialog in android?

Suppose I request a permission at runtime like the following:

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

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

  • Add search icon on action bar android
  • Android set degree symbol to Textview
  • How to use `adb` to install development apps for one user only?
  • Detect whether there is an Internet connection available on Android
  • How can I create custom controls in Android?
  • List of files of saved to the internal storage
  • Related posts:

    Determining the duration and format of an audio file
    Can an android.hardware.camera2.CaptureRequest be used with OpenCV?
    Difference between SurfaceView and GLSurfaceView in Android
    auto-scrolling TextView in android to bring text into view
    maxWidth doesn't work with fill_parent
    Bluetooth RFCOMM connection couldn't be established consecutively on Android 4.2
  • Android TabLayout Android Design
  • WARN/ActivityManager(5038): Permission denied: checkComponentPermission
  • Starting Service from BroadcastReceiver
  • Android NDK R8E missing stdlib.h
  • Android Lollipop navigation bar overlays activity view
  • Null pointer exception in suggestions popup window on Samsung Galaxy Android 7
  • 4 Solutions collect form web for “How can i customize permission dialog in android?”

    The short answer is, you can’t.

    As android documentation puts:

    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”.

    So, 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

    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 🙂

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

    You can add a Dialog if the user initially denies permission, then re ask for the permission

    public void checkPermission() {
    
        if (checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    
            // Should we show an explanation?
            if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
    
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
    
                LogUtils.log(TAG, "NO PERMISSION SHOW MESSAGE");
    
            } else {
    
                // No explanation needed, we can request the permission. The result will be captured in onRequestPermissionsResult
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
                LogUtils.log(TAG, "NO PERMISSION REQUEST PERMISSION");
            }
    
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    LogUtils.log(TAG, "REQUEST PERMISSION GRANTED :)");
    
                    getResultFromCursor();
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);
    
                    showMessageOKCancel("You need to add this Permission to see your Photos", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            LogUtils.log(TAG, "OK PRESSED");
    
                            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
                        }
                    });
    
                    LogUtils.log(TAG, "REQUEST PERMISSION DENIED :( ");
    
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    
    
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(getContext())
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }
    

    Don’t forget to change:-

    ActivityCompat.requestPermissions when in AppCompatActivity
    

    to just

    requestPermissions when in android.support.v4.app.Fragment,
    

    As the callback (onRequestPermissionsResult) will not work

    Android M Permissions: onRequestPermissionsResult() not being called

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.