1

I have bingo app where there is a GridLayout showing all the numbers done, left. I want to take a screenshot of it and then share it. I have tried many posibillities and tried to do many thing, but none of them worked

case R.id.share:
      //the answer should be here

the name of the GridLayout is gridLayout

if you need any other code of explanation, please tell me

Teerth jain
  • 75
  • 1
  • 1
  • 11
  • It's quite long process ... You need to take the view and convert it to bitmap ... and get the image check these link https://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android ...go through some other links related to it... – AgentP May 23 '20 at 14:37

1 Answers1

1

Considering that we want to take the screenshot when a button is clicked, the code will look like this:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       Bitmap bitmap = takeScreenshot();
       saveBitmap(bitmap);
   }
});

Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invoking getDrawingCache().

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}

This code is from this website The website also contains the code to save the screenshot taken.

Also question is possible duplicate of how to take a screen shot on a button click can anyone provide a android code

Prathamesh
  • 1,064
  • 1
  • 6
  • 16