Marshmallow Permission Implement In Android Studio

Android Marshmallow we need to all permission request the user for specific permissions in android. multiple permission are aloso including this code. I handle all type permission in this code

1. MainActivity

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final int PERMISSION_REQUEST_CODE = 200;
    Context context;
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11,button12,button13;


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


        Button request_permission = (Button) findViewById(R.id.request_permission);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);
        button8 = (Button) findViewById(R.id.button8);
        button9 = (Button) findViewById(R.id.button9);
        button10 = (Button) findViewById(R.id.button10);
        button11 = (Button) findViewById(R.id.button11);
        button12 = (Button) findViewById(R.id.button12);
        button13 = (Button) findViewById(R.id.button13);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button10.setOnClickListener(this);
        button11.setOnClickListener(this);
        button12.setOnClickListener(this);
        button13.setOnClickListener(this);

        request_permission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent ii = new Intent(getBaseContext(), AllPermission.class);
                startActivity(ii);
            }
        });




    }



    @Override
    public void onClick(View v) {
        if(v == button1){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissions())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissions(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button2){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionss())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionss(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button3){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsrp())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsrp(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button4){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsc())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsc(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button5){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionswc())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionswc(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button6){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsrds())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsrds(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button7){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsga())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsga(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button8){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsred())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsred(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button9){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsacl())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsacl(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button11){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionscp())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionscp(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button12){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionsxx())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionsxx(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }else if(v == button13){
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (checkPermissionszz())
                {
                    // Code for above or equal 23 API Oriented Device
                    // Your Permission granted already .Do next code
                } else {
                    requestPermissionszz(); // Code for permission
                }
            }
            else
            {

                // Code for Below 23 API Oriented Device
                // Do next code
            }
        }
    }

    //************************************ACCESS_FINE_LOCATION*********************************************************************
    private boolean checkPermissionss() {
        int result1 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (result1 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionss() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
        }
    }

//************************************************CAMERA**********************************************************************

    private boolean checkPermissions() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissions() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.CAMERA)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
        }
    }

    //************************************************READ_PHONE_STATE**********************************************************************

    private boolean checkPermissionsrp() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_PHONE_STATE);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsrp() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_PHONE_STATE)) {
            //Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************READ_CALENDAR**********************************************************************

    private boolean checkPermissionsc() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_CALENDAR);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsc() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_CALENDAR)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_CALENDAR}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************WRITE_CALENDAR**********************************************************************

    private boolean checkPermissionswc() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_CALENDAR);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionswc() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_CALENDAR)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_CALENDAR}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************READ_CONTACTS**********************************************************************

    private boolean checkPermissionsrds() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_CONTACTS);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsrds() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_CONTACTS)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************GET_ACCOUNTS**********************************************************************

    private boolean checkPermissionsga() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.GET_ACCOUNTS);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsga() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.GET_ACCOUNTS)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************RECORD_AUDIO**********************************************************************

    private boolean checkPermissionsred() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.RECORD_AUDIO);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsred() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.RECORD_AUDIO)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************ACCESS_COARSE_LOCATION**********************************************************************

    private boolean checkPermissionsacl() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsacl() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)) {
            //Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE);
        }
    }

    //************************************************CALL_PHONE**********************************************************************

    private boolean checkPermissionscp() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CALL_PHONE);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionscp() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.CALL_PHONE)) {
            //Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************READ_EXTERNAL_STORAGE**********************************************************************

    private boolean checkPermissionsxx() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionsxx() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
            //Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }
    //************************************************WRITE_EXTERNAL_STORAGE**********************************************************************

    private boolean checkPermissionszz() {
        int result2 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result2 == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermissionszz() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }


    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.e("value", "Permission Granted, Now you can use local drive .");
                }else {
                    Log.e("value", "Permission Denied, You cannot use local drive .");
                }
                break;
        }
    }


}

2. AllPermission

import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CALL_PHONE;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.GET_ACCOUNTS;
import static android.Manifest.permission.READ_CALENDAR;
import static android.Manifest.permission.READ_CONTACTS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_CALENDAR;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.Manifest.permission_group.STORAGE;

public class AllPermission extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_CODE = 200;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_permission);
        context = this;

        if (Build.VERSION.SDK_INT >= 23)
        {
            if (checkPermission())
            {
                // Code for above or equal 23 API Oriented Device
                // Your Permission granted already .Do next code
            } else {
                requestPermission(); // Code for permission
            }
        }
        else
        {

            // Code for Below 23 API Oriented Device
            // Do next code
        }


    }



    //on.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR)

    private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION);
        int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
        int result2 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_PHONE_STATE);
        int result3 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_CALENDAR);
        int result4 = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_CALENDAR);
        int result5 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_CONTACTS);
        int result6 = ContextCompat.checkSelfPermission(getApplicationContext(), GET_ACCOUNTS);
        int result7 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
        int result8 = ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION);
        int result9 = ContextCompat.checkSelfPermission(getApplicationContext(), STORAGE);
        int result10 = ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE);
        int result11 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
        int result12 = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);



        return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED
                && result3 == PackageManager.PERMISSION_GRANTED && result4 == PackageManager.PERMISSION_GRANTED && result5 == PackageManager.PERMISSION_GRANTED
                && result6 == PackageManager.PERMISSION_GRANTED && result7 == PackageManager.PERMISSION_GRANTED && result8 == PackageManager.PERMISSION_GRANTED
                && result9 == PackageManager.PERMISSION_GRANTED && result10 == PackageManager.PERMISSION_GRANTED && result11 == PackageManager.PERMISSION_GRANTED && result12 == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermission() {

        ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION, CAMERA, READ_PHONE_STATE, READ_CALENDAR, WRITE_CALENDAR, READ_CONTACTS, GET_ACCOUNTS, RECORD_AUDIO, ACCESS_COARSE_LOCATION, STORAGE, CALL_PHONE, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0) {

                    boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    boolean cameraAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                    boolean readAccepted = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                    boolean readCalendar = grantResults[3] == PackageManager.PERMISSION_GRANTED;
                    boolean writeCalendar = grantResults[4] == PackageManager.PERMISSION_GRANTED;
                    boolean readContacts = grantResults[5] == PackageManager.PERMISSION_GRANTED;
                    boolean getAccounts = grantResults[6] == PackageManager.PERMISSION_GRANTED;
                    boolean recordaudio = grantResults[7] == PackageManager.PERMISSION_GRANTED;
                    boolean accesscoarselocation = grantResults[8] == PackageManager.PERMISSION_GRANTED;
                    boolean storage = grantResults[9] == PackageManager.PERMISSION_GRANTED;
                    boolean call = grantResults[10] == PackageManager.PERMISSION_GRANTED;
                    boolean readstorage = grantResults[11] == PackageManager.PERMISSION_GRANTED;
                    boolean writestorage = grantResults[12] == PackageManager.PERMISSION_GRANTED;

                    if (locationAccepted && cameraAccepted && readAccepted && readCalendar && writeCalendar && readContacts && getAccounts && recordaudio && accesscoarselocation && storage && call && readstorage && writestorage)
                        Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access location data and camera.", Toast.LENGTH_LONG).show();




                        // Snackbar.make(view, "Permission Granted, Now you can access location data and camera.", Snackbar.LENGTH_LONG).show();
                    else {
                        Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access location data and camera.", Toast.LENGTH_LONG).show();
                        //  Snackbar.make(view, "Permission Denied, You cannot access location data and camera.", Snackbar.LENGTH_LONG).show();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {
                                showMessageOKCancel("You need to allow access to both the permissions",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                                    requestPermissions(new String[]{ACCESS_FINE_LOCATION, CAMERA, READ_PHONE_STATE, READ_CALENDAR, WRITE_CALENDAR, READ_CONTACTS, GET_ACCOUNTS, RECORD_AUDIO, ACCESS_COARSE_LOCATION, STORAGE, CALL_PHONE, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE},
                                                            PERMISSION_REQUEST_CODE);



                                                }
                                            }
                                        });
                                return;
                            }
                        }

                    }
                }


                break;
        }
    }


    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(AllPermission.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }
}

3. activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:background="#EDE7F6"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorialsee.permission.MainActivity">

    <Button
        android:id="@+id/request_permission"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3F51B5"
        android:textColor="#ffffff"
        android:text="All Request Permission"/>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="#EDE7F6"
        android:layout_below="@+id/request_permission"
        android:orientation="vertical"
        >
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button1"
                android:text="@string/button1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:textColor="#ffffff"
                android:id="@+id/button2"
                android:text="@string/button2"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button3"
                android:text="@string/button3"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:textColor="#ffffff"
                android:id="@+id/button4"
                android:text="@string/button4"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button5"
                android:text="@string/button5"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:id="@+id/button6"
                android:textColor="#ffffff"
                android:text="@string/button6"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button7"
                android:text="@string/button7"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:textColor="#ffffff"
                android:id="@+id/button8"
                android:text="@string/button8"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button9"
                android:text="@string/button9"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:textColor="#ffffff"
                android:id="@+id/button10"
                android:text="@string/button10"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:textColor="#ffffff"
                android:id="@+id/button11"
                android:text="@string/button11"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:layout_weight=".2"
                android:layout_marginLeft="4dp"
                android:textColor="#ffffff"
                android:id="@+id/button12"
                android:text="@string/button12"/>

        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="#EDE7F6"
            android:orientation="horizontal"
            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#3F51B5"
                android:textColor="#ffffff"
                android:id="@+id/button13"
                android:text="@string/button13"/>


        </LinearLayout>
    </LinearLayout>
</RelativeLayout>