In this video it shows the code to print the complete content of a WebView widget. It calls the Print API of the PrintManager class. In this demonstration it prints the content of our website’s home page: https://programmerworld.co/
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.printthewebview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.view.View;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.loadUrl("https://programmerworld.co/");
}
public void buttonPrint(View view){
PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
PrintDocumentAdapter printDocumentAdapter = webView.createPrintDocumentAdapter("Print1");
printManager.print("PrintTest1", printDocumentAdapter, new PrintAttributes.Builder().build());
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PrintTheWebView"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="139dp"
android:layout_marginTop="58dp"
android:onClick="buttonPrint"
android:text="Print"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/webView"
android:layout_width="335dp"
android:layout_height="528dp"
android:layout_marginStart="38dp"
android:layout_marginTop="36dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Excerpt:
The content provides a video demonstration of the code to print the complete content of a WebView widget utilizing the Print API from the PrintManager class in Android development. The demonstration specifically shows how to print the content of the home page of the ‘programmerworld.co’ website. It also provides the complete source code for this operation, including definitions for the MainActivity class, the manifest file, and the XML layout file. For further queries, suggestions, or feedback, it directs users to contact via the website contact form or email.