@Override public void onPageFinished(WebView view, String url) { print(name, view.createPrintDocumentAdapter(), new PrintAttributes.Builder().build()); } });
private void print() { // Get the print manager. PrintManager printManager = (PrintManager) getSystemService( Context.PRINT_SERVICE); // Pass in the ViewView's document adapter. printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), null); } }
private void print() { // Get the print manager. PrintManager printManager = (PrintManager) getSystemService( Context.PRINT_SERVICE); // Pass in the ViewView's document adapter. printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), null); } }
/** * Print a {@link WebView}'s contents, also allows to create a PDF * * @param webview WebView * @param jobName Name of the job (affects PDF name too) * @return {{@link PrintJob}} or null */ @RequiresApi(api = Build.VERSION_CODES.KITKAT) @SuppressWarnings("deprecation") public PrintJob print(WebView webview, String jobName) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { PrintDocumentAdapter printAdapter; PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { printAdapter = webview.createPrintDocumentAdapter(jobName); } else { printAdapter = webview.createPrintDocumentAdapter(); } if (printManager != null) { return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build()); } } else { Log.e(getClass().getName(), "ERROR: Method called on too low Android API version"); } return null; }
@Override public boolean onOptionsItemSelected(MenuItem item) { Intent intent; int itemId = item.getItemId(); if (itemId == android.R.id.home) { intent = new Intent(this, AboutActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; } else if (itemId == R.id.menu_print) { PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); PrintDocumentAdapter adapter = webView.createPrintDocumentAdapter(); printManager.print(getString(R.string.app_name), adapter, null); return true; } return false; }