title: My Permissions Interface Is Not Working Properly For Android API 24 And Upwards
body: <p>I want to create a code that as soon the app begins, it asks for permissions ( WRITE and CAMERA ). If one or all permissions are denied then the app shows a warning text (in TextView ) asking for permissions and also a button “Permission” ( also made from a TextView ) that WILL REMAIN VISIBLE while the user keeps denying any or all these permissions. </p>
  
  <p>This app prototype works fine in the emulator API 23, but in the emulator API 24 and upwards it did not work properly.</p>
  
  <p>Here are the codes:</p>
  
  <p>1) AndroidManifest.xml</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.lf.permissionsfromlf"&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-feature android:name="android.hardware.camera2" /&gt;
      &lt;uses-feature android:name="android.hardware.camera"
          android:required="true" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>2) activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
      &lt;TextView
          android:id="@+id/permissionNote"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="24dp"
          android:gravity="center"
          android:letterSpacing="0.1"
          android:text="@string/give_permission"
          android:textColor="#000066"
          android:textSize="16sp"
          android:textStyle="bold"
          android:visibility="invisible"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
      &lt;TextView
          android:id="@+id/permission_btn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:background="@drawable/permission_btn"
          android:gravity="center"
          android:letterSpacing="0.1"
          android:text="@string/permission"
          android:textColor="#ffffff"
          android:textSize="20sp"
          android:textStyle="bold"
          android:visibility="invisible"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.50"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/permissionNote" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>3) MainActivity.java</p>
  
  <pre><code>import android.Manifest;
  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.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity
  {
      TextView permissionNote;
      TextView permission_btn;
      boolean WRITE_GRANTED = false;
      boolean CAMERA_GRANTED = false;
      static final int REQUEST_PERMISSION_CODE = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ///////////
          permissionNote =  findViewById(R.id.permissionNote);
          permission_btn =  findViewById(R.id.permission_btn);
          permissionNote.setVisibility(View.INVISIBLE);
          permission_btn.setVisibility(View.INVISIBLE);
  
          //Sets OnClick for the PERMISSION BUTTON in case it becomes necessary
          permission_btn.setOnClickListener(new TextView.OnClickListener()
          {
              @Override
              public void onClick(View v)
              {
                  if( !WRITE_GRANTED || !CAMERA_GRANTED )
                  request_permission();
              }
          });
  
          //VERIFY IF PERMISSIONS ARE GRANTED for SDK_INT &gt;= 23
          if( Build.VERSION.SDK_INT &gt;= 23 )
          {
              //Verifies if permission was already granted
              if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                      != PackageManager.PERMISSION_GRANTED) ||
                      (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                              != PackageManager.PERMISSION_GRANTED))
              {
                  permissionNote.setVisibility(View.VISIBLE);
                  permission_btn.setVisibility(View.VISIBLE);
                  request_permission();
              }
              else
              {
                  // Permission has already been granted
                  permissionNote.setVisibility(View.INVISIBLE);
                  permission_btn.setVisibility(View.INVISIBLE);
              }
              //////////////////////
              if( WRITE_GRANTED &amp;&amp; CAMERA_GRANTED )
              {
                  // Permission has already been granted
                  permissionNote.setVisibility(View.INVISIBLE);
                  permission_btn.setVisibility(View.INVISIBLE);
              }
              //////////////////////
          }
      }
  
      public void request_permission()
      {
          if(!WRITE_GRANTED)
          {
              ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                      REQUEST_PERMISSION_CODE);
          }
          if(!CAMERA_GRANTED)
          {
              ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.CAMERA},
                      REQUEST_PERMISSION_CODE);
          }
          ////////////////////////
          // IF ALL PERMISSIONS ARE GRANTED, MAKE PERMISSION BUTTON INVISIBLE
          if(write_permission_granted())
          {
              WRITE_GRANTED = true;
          }
          if(camera_permission_granted())
          {
              CAMERA_GRANTED = true;
          }
  
      }
  
  @Override
  public void onRequestPermissionsResult(int requestCode,
                                         String permissions[], int[] grantResults)
  {
      switch (requestCode) {
          case REQUEST_PERMISSION_CODE:
          {
              // If request is cancelled, the resulting arrays is empty.
              if (grantResults.length &gt; 0
                      &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED)
              {
                  // Calling write_permission_granted and camera_permission_granted() after
                  // onRequestPermissionsResult
                  // updates write_permission and camera permission as to make the PERMISSION BUTTON invisible
                  if(write_permission_granted() &amp;&amp; camera_permission_granted()) //CAMERA_GRANTED = true;
                  {
                      permissionNote.setVisibility(View.INVISIBLE);
                      permission_btn.setVisibility(View.INVISIBLE);
                  }
              }
  
              return;
          }
      }
  
  }
  
  
  
  
      public boolean write_permission_granted()
      {
          if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                  == PackageManager.PERMISSION_GRANTED) return true;
          else return false;
      }
  
      public boolean camera_permission_granted()
      {
          if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                  == PackageManager.PERMISSION_GRANTED) return true;
          else return false;
      }
  }
  </code></pre>
  
  <p>So the idea is that the warning and the button will remain visible in the
   screen in case the user changes his mind and clicks the button in order to give
   all permissions. Once all the permissions are given then immediately the
   warning and the button SHOULD DISAPPEAR ( become invisible ).</p>
  
  <p>In the API 24 emulator, when the app starts, only a popup box with the question
   “Allow the app to access photos, media, and files on your device?” appears.
   After answering this one, it  should also popup a second box with the question
   ”Allow this app to take pictures and record video?” BUT IT DOES NOT HAPPEN. As
   consequence, the warning and the button “PERMISSION” remain on the screen. 
  So, after that, when I click on this button in the first time it does not respond.
   In the second time when I click again it finally pops up the box 
  “Allow this app to  take pictures...”. Only after this second permission is
   finally given, only then the warning and the button disappear ( become
   invisible ) but this should  have happened right at the start of the app.</p>