title: How To Programmatically take a screenshot in android of Alert Dialog
body: <p>I've seen the following <a href="https://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android">Link</a> and it does take a screenshot with the top answer</p>
  <p>However, what I want is for the app to take a screenshot of the Alert Dialog that I am displaying to the user, the above solution and below code only takes a screenshot of what is currently behind the alert dialog and therefore no good</p>
  <p>Here is the code being used in case anyone hasn't gone through the link provided</p>
  <pre><code>Date now = new Date();
      android.text.format.DateFormat.format(&quot;yyyy-MM-dd_hh:mm:ss&quot;, now);
  
      try {
          // image naming and path  to include sd card  appending name you choose for file
          String mPath = Environment.getExternalStorageDirectory().toString() + &quot;/&quot; + now + &quot;.jpg&quot;;
  
          // create bitmap screen capture
          View v1 = getWindow().getDecorView().getRootView();
          v1.setDrawingCacheEnabled(true);
          Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
          v1.setDrawingCacheEnabled(false);
  
          File imageFile = new File(mPath);
  
          FileOutputStream outputStream = new FileOutputStream(imageFile);
          int quality = 100;
          bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
          outputStream.flush();
          outputStream.close();
  
          openScreenshot(imageFile);
      } catch (Throwable e) {
          // Several error may come out with file handling or OOM
          e.printStackTrace();
      }
  </code></pre>
  <p>EDIT: code for dialog as requested</p>
  <pre><code>public void showCalc(String title, String message) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setCancelable(true);
      builder.setTitle(title);
      builder.setMessage(message);
      builder.setPositiveButton(&quot;Capture + Open&quot;,
              new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                      //Remove Values From Inventory
                      captureScreenAndOpen();
  
                  }
              });
  
      builder.setNegativeButton(&quot;Capture&quot;,
              new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                      captureScreen();
                      Context context = getApplicationContext();
                      Toast.makeText(context, &quot;Screenshot Captured&quot;, Toast.LENGTH_LONG).show();
                  }
              });
  
      builder.setNeutralButton(&quot;Return&quot;, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
          }
      });
      builder.show();
  }
  </code></pre>
  <p>FURTHER EDIT:</p>
  <p>Here you will see two screenshots, the first is showing the saved screenshot when everything is saved in the screenshot from the dialog, you'll notice at the bottom there is a bit of text which is always present at the bottom.</p>
  <p><a href="https://i.stack.imgur.com/246fH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/246fH.jpg" alt="Screenshot1" /></a></p>
  <p>The second screenshot is where there is so much text in the dialog the dialog is scrollable so that you can see all the data, you'll notice that the bottom string in the first screenshot is not present</p>
  <p><a href="https://i.stack.imgur.com/epUfA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/epUfA.jpg" alt="Screenshot2" /></a></p>
  <p>If possible I'd like it that all the data is displayed, I'm not sure though if a screenshot function would be able to do this or an alternative method</p>