Search code examples
androidscreenshotandroid-softkeyboard

capture activity screen with on-screen keyboard


I am trying to capture my activity's screen on android. I am using the code below:

View root = this.activity.getWindow().getDecorView().getRootView();

Bitmap b = Bitmap.createBitmap(root.getWidth(), root.getHeight(), Config.RGB_565);
Canvas c = new Canvas(b);
root.draw(c);

if (b != null && !b.isRecycled()) {
    this.screen = Bitmap.createBitmap(b);
}

If on-screen keyboard pops up, then I've got only visible part of my activity's window in this.screen and the rest is blank.

Is there any way to get my app's screenshot, including the parts, that are under on-screen keyboard?


Solution

  • I'm sorry to dissapoint you, but this cannot be done without external aid. The keyboard is simply not a part of your application layout.

    Your "outermost" view is the DecorView, which you are already snapping. That's as far as this technique goes.

    Edit: I can't try it now, but give this a go:

    View topmost = ((ViewGroup)
         ctx.getWindow().getDecorView().findViewById(android.R.id.content)
    ).getChildAt(0);
    
    topmost.setDrawingCacheEnabled(true);
    
    Bitmap screenshot = topmost.getDrawingCache();