Android Request Permission at Runtime Example

Today we will see a simple Android Request Permission at Runtime Example. We have already covered this topic before. But the previous way of requesting permissions in runtime is obsolete. In this post, we will see the most updated way of requesting runtime permission.

You can also check the old way of requesting runtime permissions.

But the new method is very, very easy, and simplified.

Now, let’s start our project without wasting any more time.

Android Request Permission at Runtime Example

As always, we will create a new project using an Empty Activity. Once you are done creating your project, we can start adding the required dependencies.

Adding Dependencies

Here is the app level build.gradle  file of my project.

Here we need to add Java8 compile options, as the new things require some Java8 features. Also, we have added two dependencies that will simplify requesting permission.

Declaring Permission in AndroidManifest.xml

We also need to declare all the permissions required in AndroidManifest.xml file.

Designing UI

As it is just an example; we will create a very simple UI. Below you can see what I have designed.

Android Request Permission at Runtime Example
Android Request Permission at Runtime Example

So design is very simple; we only have a couple of TextViews and a Button. (Don’t worry if you have any problem; you can get the complete source code at the end of this post).

We are halfway done for this Android Request Permission at Runtime Example. I will create an extension function to show an explanation alert to the user that why do we need this permission.

Requesting Permission at Runtime

Now let’s request permission, and here is my MainActivit.kt .

Here is the pseudocode to explain what needs to be done for asking permission.

  1. User requested a feature that requires a specific permission (For example we need to Save a File, and it requires Write Storage Permission).
  2. Check if that permission is already granted or not using
  3. If permission is granted; that’s it you can go with the flow.
  4. If permission is not already given; check whether user was previously asked for this permission or not using showPermissionRequestExplanation()  function.
  5. If the permission was previously asked and denied by the user; show the user an explanation that why do this permission is needed.
  6. To request permission use

That’s it. Easy Right?

Android Request Permission at Runtime Example Source Code

Still confused? You can check my source code here.

That is all for this Android Request Permission at Runtime Example. In case you have any confusion don’t hesitate to leave your comments below. Thank You 🙂

1 thought on “Android Request Permission at Runtime Example”

Leave a Comment