// setting clickListener for Save Pdf Button
savePdfBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(printWeb!=null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Calling createWebPrintJob()
PrintTheWebPage(printWeb);
}else
{
// Showing Toast message to user
Toast.makeText(MainActivity.this, "Not available for device below Android LOLLIPOP",
Toast.LENGTH_SHORT).show();
}
}
else
{
// Showing Toast message to user
Toast.makeText(MainActivity.this, "WebPage not fully loaded", Toast.LENGTH_SHORT).show();
}
}
});
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void PrintTheWebPage(WebView webView) {
// set printBtnPressed true
printBtnPressed=true;
// Creating PrintManager instance
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
// setting the name of job
String jobName = getString(R.string.app_name) + " webpage"+webView.getUrl();
// Creating PrintDocumentAdapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);
// Create a print job with name and adapter instance
assert printManager != null;
printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
@Override
protected void onResume() {
super.onResume();
if(printJob!=null &&printBtnPressed) {
if (printJob.isCompleted()) {
// Showing Toast Message
Toast.makeText(this, "Completed", Toast.LENGTH_SHORT).show();
} else if (printJob.isStarted()) {
// Showing Toast Message
Toast.makeText(this, "isStarted", Toast.LENGTH_SHORT).show();
} else if (printJob.isBlocked()) {
// Showing Toast Message
Toast.makeText(this, "isBlocked", Toast.LENGTH_SHORT).show();
} else if (printJob.isCancelled()) {
// Showing Toast Message
Toast.makeText(this, "isCancelled", Toast.LENGTH_SHORT).show();
} else if (printJob.isFailed()) {
// Showing Toast Message
Toast.makeText(this, "isFailed", Toast.LENGTH_SHORT).show();
} else if (printJob.isQueued()) {
// Showing Toast Message
Toast.makeText(this, "isQueued", Toast.LENGTH_SHORT).show();
}
// set printBtnPressed false
printBtnPressed=false;
}
}