Reputation: 35
I have to design an app in which on click of the neutral button, an image of the alert dialogue gets stored in the sdcard. So I decided to programatically take a screenshot and then use the BitmapDecodeRegion class to crop the image.
But when i take the screenshot, the alert dialogue does not appear in it since it's not attached to the window. How can I attach it to the window?
here is the code snippet:
public void btnClick(View v) {
Log.d("", "logger button clicked");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("This is a demo!");
dialog.setMessage("Lets see if this works");
dialog.setCancelable(false);
dialog.setNeutralButton("Take Snap",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bitmap bitmap;
View v1 = findViewById(android.R.id.content)
.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
GlobalObj.screenImg = bitmap;
v1.setDrawingCacheEnabled(false);
Intent i = new Intent(MainActivity.this,
ViewActivity.class);
startActivity(i);
}
});
AlertDialog newDialog = dialog.create();
newDialog.show();
Kindly help me out.
Upvotes: 1
Views: 1040
Reputation: 1135
In your code change this line:
View v1 = findViewById(android.R.id.content)
.getRootView();
to this:
View v1 = AlertDialog.class.cast(dialog).getWindow().getDecorView().getRootView();
It works for me and does not need rooted phone. However this captures only the alert dialog on a black background, without the underlying views from your activity. If you want to have everything (not that simple) you can try this answer from Aswin Rajendiran.
Upvotes: 1
Reputation: 1301
Try out this library: https://github.com/jraska/Falcon. It takes screenshots of all active windows of application including dialogs and could figure out your problem.
Upvotes: 1