6

What is missing in this code? This same code works on ICS. On API 8 the scroll appears and some content goes out of screen. How to get the drawing cache in this case?

Code:

TableLayout page = (TableLayout) findViewById(R.id.page);
page.setDrawingCacheEnabled(true);
page.buildDrawingCache();

// getDrawingCache returns null...
Bitmap pageBmp = Bitmap.createBitmap(page.getDrawingCache(true));
page.destroyDrawingCache();
page.setDrawingCacheEnabled(false);
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Possible duplicate of [Android View.getDrawingCache returns null, only null](http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null) – Yuliia Ashomok Apr 13 '16 at 10:47
  • similar http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null – Yuliia Ashomok Apr 13 '16 at 10:47

1 Answers1

18

I solved it. Created a bitmap of view size and drew the view into it.

TableLayout page = (TableLayout) findViewById(R.id.page);
Bitmap pageBmp = Bitmap.createBitmap(page.getWidth(), page.getHeight(), 
                                       Config.ARGB_8888);
Canvas canvas = new Canvas(pageBmp);
page.draw(canvas);

Used the pageBmp bitmap..

Ron
  • 24,175
  • 8
  • 56
  • 97
  • 10x a lot man, i had some problems with a bitmap, it was always null, with something similar as in how you tried the first time, but this code just saved my project :D It works flawless – rosu alin Aug 20 '12 at 08:03
  • Awesome hack dude.... Solved my problem. This would really help those who are using scrollview as a parent because getDrawingCache() doesn't work if scrollView is present. – Irfan Raza Sep 25 '16 at 22:39