I need to take screenshot programmatically of activity view. I have found a lot of answers how to do it, but there is not answer how to do it with opened dialog
This is work for me (but without dialog):
public class MainActivity extends AppCompatActivity {
private View main;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here we Checking CALL_PHONE permission
setContentView(R.layout.activity_main);
main = findViewById(R.id.main);
imageView = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Bitmap b = Screenshot.takescreenshotOfRoot(imageView);
imageView.setImageBitmap(b);
main.setBackgroundColor(Color.parseColor("#999999"));
}
});
}
}
Screenshot class:
public class Screenshot {
public static Bitmap takescreenshot (View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takescreenshotOfRoot(View v) {
return takescreenshot(v.getRootView());
}
}
XML: