Changes

Jump to: navigation, search

An Android Studio HTML and Web Content Printing Example

3,932 bytes removed, 19:45, 6 June 2014
no edit summary
Figure 50-4
 
== Adding the Print Menu Option ==
 
The option to print the web page will now be added to the Overflow menu using the techniques outlined in the chapter entitled Creating and Managing Overflow Menus on Android.
 
The first requirement is a string resource with which to label the menu option. Within the Project tool window, locate the src -> main -> res -> values -> strings.xml file, double click on it to load it into the editor and modify it to add a new string resource:
 
<pre>
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
<string name="app_name">WebPrint</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="print_string">Print</string>
 
</resources>
</pre>
 
Next, locate and edit the src -> main -> res -> menu -> web_print.xml file and replace the Settings menu option with the print option:
 
<pre>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".WebPrintActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
 
<item
android:id="@+id/action_print"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/print_string"/>
 
</menu>
</pre>
 
All that remains in terms of configuring the menu option is to modify the onOptionsItemSelected() handler method within the WebPrintActivity.java file:
 
<pre>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_print) {
createWebPrintJob(myWebView);
return true;
}
return super.onOptionsItemSelected(item);
}
</pre>
 
With the onOptionsItemSelected() method implemented, the activity will call a method named createWebPrintJob() when the print menu option is selected from the overflow menu. The implementation of this method is identical to that used in the previous HTMLPrint project and may now be added to the WebPrintActivity.java file such that it reads as follows:
 
<pre>
package com.ebookfrenzy.webprint.webprint;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.content.Context;
 
public class WebPrintActivity extends Activity {
 
private WebView myWebView;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_print);
 
myWebView = (WebView) findViewById(R.id.myWebView);
 
myWebView.loadUrl(
"http://developer.android.com/google/index.html");
}
 
private void createWebPrintJob(WebView webView) {
 
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
 
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
 
String jobName = getString(R.string.app_name) +
" Print Test";
 
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
.
.
}
</pre>
 
With the code changes complete, run the application on a physical Android device running Android version 4.4 or later. Once successfully launched, the WebView should be visible with the designated web page loaded. Once the page has loaded, select the Print option from the Overflow menu (Figure 50-4) and use the resulting print panel to print the web page to a suitable destination.
 
[[Image:android_print_menu_option2_reduced.png|The Print menu option added to the Overflow menu of an Android app]]
 
Figure 50-4
 
== Summary ==
The Android Printing framework includes extensions to the WebView class that make it possible to print HTML based content from within an Android application. This content can be in the form of HTML created dynamically within the application at runtime, or a pre-existing web page loaded into a WebView instance. In the case of dynamically created HTML, it is important to use a WebViewClient instance to ensure that printing does not start until the HTML has been fully loaded into the WebView.

Navigation menu