Search code examples
javaandroidgrid-layout

How to take a screenshot of a GridLayout and then share it


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


Solution

  • 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