Android Studio Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

how to request permission in android programmatically

AndroidManifest.xml

<!-- Add Write Permissions in the Manifest File-->
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

    <!-- Add Read Permissions in the Manifest File-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

AndroidManifest.xml

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 102;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // you need to request permissions if Android Version is targeting 6 (Marshmallow) or Above
        // below 6 (Marshmallow) version you don't need to request for permissions at runtime
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            // check if permissions are already granted or not. If not then request for permissions
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                // requesting permissions, you can add multiple permissions separated with comma(,)
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

            } else {
                // permissions granted. you code goes here
            }
        } else {
            // permissions granted. you code goes here
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE) {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(this, "Permissions Denied", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Accepted", Toast.LENGTH_SHORT).show();
                // permissions granted. you code goes here
            }
        }
    }
}

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

HD-Wallpaper-App-in-Android-Studio-----------
HD Wallpaper App in Android Studio
Online-Tic-Tac-Toe-game-Using-Firebase-Database-in-Android-Studio
Online Tic Tac Toe game Using Firebase Database in Android Studio
Sound-Recorder-App-in-Android-Studio-----------
Sound Recorder App in Android Studio

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.