How to take screenshot of dialog in Android programmatically?
<p>I have Android application which interacts with Web Server. When data is sent from phone to server, there is a bit of work with data (10-20 secs) and after that I get result back to phone. And i display that result in dialog with 2 choices. One choice is "OK" and it means that user is fine with result and dismiss the dialog. The other choice makes a screenshot of that results. And when I press this button I don't get picture of dialog, i get picture of layout which is behind that dialog.</p>

<p><a href="https://i.stack.imgur.com/ztQXy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ztQXy.png" alt="enter image description here"></a></p>

<p>My code behind this is </p>

<pre><code>//this method is called on onPostExecute of async task

public void recieveResult (final Activity act,String result){
    new AlertDialog.Builder(act)
            .setTitle("Data - status")
            .setMessage(result)
            .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setPositiveButton("Save picture", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Bitmap bitmap = takeScreenshot(act);
                    saveBitmap(bitmap,act);
                    Toast.makeText(act,"Saved",Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }
            })
            .show();
}

public Bitmap takeScreenshot(Activity act) {
    View rootView = act.findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap,Activity act) {
    String timeStamp = new SimpleDateFormat("MMyyyydd_HHmmss").format(new Date());
    String imageFileName = "/screenshot_"+timeStamp+"_.png";
    File storage = Environment.getExternalStoragePublicDirectory("Screens");
    if (!storage.exists())
    {
        Toast.makeText(act, "Directory made", Toast.LENGTH_LONG).show();
        storage.mkdirs();
    }
    File imagePath = new File(storage.getPath() + imageFileName);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
</code></pre>

<p>My result of picture is just screen without dialog. How to get picture of dialog in it? </p>