Ask Runtime Permission in Kotlin Android

As we all know from API level 23 (Android 6.0) and above, we need to ask Runtime Permissions in Android App. The following article contains utility methods to ask runtime permission in Kotlin Android. This code also shows an Alert Dialog to the user to allow permissions from App Settings, if the user denies permission in App. The screenshots are the following.

Android categories permissions into 2 major types, Normal and Dangerous permissions. For normal permissions, we don’t need to ask them at runtime, only declaration in the AndroidManifest.xml file is enough. But for dangerous permissions, only declaration in the AndroidManifest file is not enough, we also need to ask them at runtime. At this Link, you can check the list of normal & dangerous permissions.

Utility Methods to Ask Runtime Permissions in Kotlin

Add the following methods in your Activity or Fragment, where you want to ask runtime permissions. In this code, READ_EXTERNAL_STORAGE permission is considered. You can change it with your required permission.

    fun isPermissionsAllowed(): Boolean {
        return if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            false
        } else true
    }

    fun askForPermissions(): Boolean {
        if (!isPermissionsAllowed()) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this as Activity,Manifest.permission.READ_EXTERNAL_STORAGE)) {
                showPermissionDeniedDialog()
            } else {
                ActivityCompat.requestPermissions(this as Activity,arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),REQUEST_CODE)
            }
            return false
        }
        return true
    }

    override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<String>,grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CODE -> {
                if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission is granted, you can perform your operation here
                } else {
                    // permission is denied, you can ask for permission again, if you want
                   //  askForPermissions()
                }
                return
            }
        }
    }

    private fun showPermissionDeniedDialog() {
        AlertDialog.Builder(this)
            .setTitle("Permission Denied")
            .setMessage("Permission is denied, Please allow permissions from App Settings.")
            .setPositiveButton("App Settings",
                DialogInterface.OnClickListener { dialogInterface, i ->
                    // send to app settings if permission is denied permanently
                    val intent = Intent()
                    intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                    val uri = Uri.fromParts("package", getPackageName(), null)
                    intent.data = uri
                    startActivity(intent)
                })
            .setNegativeButton("Cancel",null)
            .show()
    }

This is how to call the above method

if (askForPermissions()) {
   // Permissions are already granted, do your stuff
}

That’s it. Enjoy šŸ™‚

You can also find other useful helping articles, code snippets and tutorials in our Coding Articles & Tutorials Knowledge Base.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

4 thoughts on “Ask Runtime Permission in Kotlin Android

  1. Iā€™d must examine with you here. Which is not one thing I usually do! I take pleasure in reading a publish that can make individuals think. Also, thanks for permitting me to comment!

  2. When someone writes an article he/she maintains the thought of a user in his/her mind
    that how a user can know it. Therefore that’s why this article is great.
    Thanks!

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: