Learn Here To request runtime permission on Activity or App Start or Button Click in Android Studio Latest Version Step By Step with Easy Tutorial.
In this tutorial, we will request runtime android permission on Button click using a library or dependencies.
Before Starting the Project please check that you have the latest version of Android Studio on your PC or Laptop either in Windows or Linux, Coding will be the same for all projects on any OS under Android Studio. Also, Check that you must have a stable Internet Connection otherwise your project will not sync and you can’t run it. And Also please disable the Antivirus if you have Installed it in Your System.
First of all, open your Android Studio then start a new Android Studio project as shown in below screenshot:

You will see: Select a Project Template Window then Select Empty Activity and click on the Next button as shown in the below screenshot:

Now you will see: Configure Your Project Window then Fill up your App Name (Any Name you can put), then select Package Name (Package name is the unique id which is used as play store URL when you publish it. So it should be unique and in small letters and no space). Then enter your project location where you want to save it. Then Choose Language “Java” or “Kotlin”, here we will use Java in our Project. Finally, Choose Minimum SDK Version 19 then click on Finish Button. See the below screenshot for more details:

Now you will see that your project is start to sync or build. Don’t click anywhere when your Android Studio is under sync or build condition other the process may failed and you will face errors.
After project sync you ready to go for changing or building your app.
Now first of all we have to add library to our project, We will add Dexter Library for Runtime Permission in our Project which is very easy to implement.
copy below line of code and paste it in build.gradle file:
implementation 'com.karumi:dexter:6.2.2'
Add the below Permission to manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.BLUETOOTH"/>
Add the below line of code before the onCreate
private static final int PERMISSION_REQUEST_CODE = 200;
After that you have to add following code to after the and tag of onCreate:
private void requestPermission() { ActivityCompat.requestPermissions(MainActivity.this, new String[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, READ_SMS, BLUETOOTH, ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_CODE) { if (grantResults.length > 0) { boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; if (storageAccepted) { Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Permission Denied", Toast.LENGTH_LONG).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) { showMessageOKCancel( new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } }); } } } } } } private void showMessageOKCancel(DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(MainActivity.this) .setMessage("Please allow all the permissions") .setPositiveButton("OK", okListener) .setNegativeButton("Cancel", null) .create() .show(); }
Where Error Appears… Just Keep the mouse cursor on it and simply press the Left Alt + Enter Together, again enter if asked.
And finally call the Request Permission Dialog where you want:
Add a button to your layout file and add the below code in MainActivity.java on button click method or listener
requestPermission();
Now run your project and you are good to go… Please share this post to your friends on social media.
You can download the full source code for this tutorial from below link.