Printing documents directly from a WebView in an Android app can be a highly useful feature, especially for apps that display web content. Whether you need to print tickets, receipts, or reports, this guide will walk you through the process step-by-step.
Prerequisites
- Basic knowledge of Java and JavaScript
- An existing Android project with a WebView component
Step-by-Step Guide
1. Setting Up Your HTML and JavaScript
First, you need to set up a button in your HTML that will trigger the print command when clicked. Here’s how you can do it:
<!DOCTYPE html> <html> <head> <title>Print Example</title> </head> <body> <button onclick="printDocument()">Print Document</button> <script> function printDocument() { // Send a message to the Android app to print if (window.Android) { window.Android.print(); } else { console.log("Android interface not found."); } } </script> </body> </html>
2. Enabling JavaScript in WebView
In your MainActivity.java
, you need to set up the WebView and enable JavaScript:
import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient()); // Load your site webView.loadUrl("https://your-site.com"); // Add JavaScript interface webView.addJavascriptInterface(new WebAppInterface(this), "Android"); } public class WebAppInterface { MainActivity mContext; WebAppInterface(MainActivity c) { mContext = c; } @JavascriptInterface public void print() { runOnUiThread(new Runnable() { @Override public void run() { createWebPrintJob(webView); } }); } } private void createWebPrintJob(WebView webView) { // Get a PrintManager instance PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE); // Get a print adapter instance PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter("MyDocument"); // Create a print job with name and adapter instance String jobName = getString(R.string.app_name) + " Document"; printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build()); } }
3. Understanding the Code
JavaScript: When the "Print Document" button is clicked, the printDocument
function is called. This function checks if the Android interface is available (window.Android
) and calls the print
method on it.
Java: In MainActivity.java
, we enable JavaScript in the WebView settings. We then add a JavaScript interface (WebAppInterface
) that listens for the print
function call from JavaScript. When called, it runs the print job on the main thread using createWebPrintJob(webView)
.
4. Printing the Document
The createWebPrintJob
method gets a PrintManager
instance and creates a print job with the WebView content. It uses webView.createPrintDocumentAdapter
to create a print adapter and starts the print job.
Conclusion
By following these steps, you can enable print functionality in your WebView Android app. This allows users to print documents directly from the web content displayed in the app, enhancing the usability and functionality of your application.
Feel free to reach out if you have any questions or need further assistance. Happy coding!
Leave a Reply