c# - Screenshots of multiple actionbar tabs -
i using xamarin , c# suspect problem equally valid in java environment.
i have actionbar activity hosts 3 tabs each of hosts fragment. uses viewpager allow user swipe between tabs.
the requirement programmatically screenshot each tab , email these attachments.
the problem whilst actionbar/viewpager works optimises tabs - isn't creating fragment's view until next in line shown. so, if you're on tab 0 - first tab - fragment view tab 2 null. can't screenshot.
to overcome have tried set tab/fragment has null view selected. generates view because setting selected not render on screen view not have width or height value again cannot screenshot (this reason defensive check @ start of code taking screenshot).
so, guess question how can force tab rendered on screen correctly filled out , can screenshot?
my main code extracts follows:
private void emailreport() { list <bitmap> bitmaps = new list<bitmap>(); list <string> summaryfiles = new list<string>(); // remember tab we're on var selectedtab = this.actionbar.selectednavigationindex; // take screenshots (int fragmentnumber = 0; fragmentnumber < projectfragmentpageradapter.count; fragmentnumber++) { android.support.v4.app.fragment fragment = projectfragmentpageradapter.getitem(fragmentnumber); if (fragment.view == null) { this.actionbar.gettabat(fragmentnumber).select(); fragment = projectfragmentpageradapter.getitem(fragmentnumber); } bitmaps.add(screenshot(fragment.view)); } // set active tab this.actionbar.gettabat(selectedtab).select(); //write screenshots file int = 0; foreach(bitmap bitmap in bitmaps) { if (bitmap != null) summaryfiles.add(bitmaptofile(bitmap, this.actionbar.gettabat(i).text)); i++; } // send file emailsupport.sendattachments(this, summaryfiles); } private bitmap screenshot(view fragmentrootview) { if (fragmentrootview == null || fragmentrootview.width == 0 || fragmentrootview.height == 0) return null; fragmentrootview.drawingcacheenabled = true; //create bitmap layout , draw view bitmap bitmap = bitmap.createbitmap(fragmentrootview.width, fragmentrootview.height,bitmap.config.argb8888); canvas canvas = new canvas(bitmap); //get view's background drawable bgdrawable = fragmentrootview.background; if (bgdrawable!=null) // has background drawable, draw on canvas bgdrawable.draw(canvas); else // not have background drawable, draw white background on canvas canvas.drawcolor(color.white); // draw view on canvas fragmentrootview.draw(canvas); fragmentrootview.drawingcacheenabled = false; return bitmap; }
any gratefully received.
the solution in end simple. viewpager has setting controlling number of pages (fragments) hold "activated". defaults 1. had 3 tabs meant there 1 tab (fragment) out of reach.
so, whilst setting viewpager following before tabs added:
reportviewpager.offscreenpagelimit = pagecount - 1;
or in java
reportviewpager.setoffscreenpagelimit(pagecount - 1);
i hope helps else avoid wasting hours.
Comments
Post a Comment