Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 447
nullpointer Avatar asked Oct 21 '15 18:10

nullpointer


People also ask

How do I set custom permissions in Android dialog?

requestPermissions(thisActivity, new String[]{Manifest. permission. READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); Android system creates a popup dialog to request permission.

How do I set custom permissions on Android?

You can use the sharedUserId attribute in the AndroidManifest. xml 's manifest tag of each package to have them assigned the same user ID. By doing this, for purposes of security the two packages are then treated as being the same app, with the same user ID and file permissions.

What are two types of permissions in Android?

Android categorizes permissions into different types, including install-time permissions, runtime permissions, and special permissions.

How many types of permissions are there in Android?

The three permission protection levels in Android are as follows: Normal Permissions. Signature Permissions. Dangerous Permissions.


2 Answers

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

like image 81
yrazlik Avatar answered Sep 24 '22 18:09

yrazlik


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 :)

like image 43
Anand Savjani Avatar answered Sep 24 '22 18:09

Anand Savjani