An Android Studio HTML and Web Content Printing Example

From Techotopia
Revision as of 16:51, 26 December 2015 by Neil (Talk | contribs) (Creating the Web Page Printing Example)

Jump to: navigation, search
PreviousTable of ContentsNext
Printing with the Android Printing Framework in Android StudioAn Android Studio Custom Document Printing Example


<google>BUY_ANDROID_STUDIO</google>


As outlined in the previous chapter, entitled Printing with the Android Printing Framework, the Android Printing framework can be used to print both web pages and dynamically created HTML content. Whilst there is much similarity in these two approaches to printing, there are also some subtle differences that need to be taken into consideration. This chapter will work through the creation of two example applications in order to bring some clarity to these two printing options.

Creating the HTML Printing Example Application

Begin this example by launching the Android Studio environment and creating a new project, entering HTMLPrint into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.

On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 19: Android 4.4 (KitKat). Continue to proceed through the screens, requesting the creation of a blank activity named HTMLPrintActivity with a corresponding layout named activity_html_print and menu resource file named menu_html_prin.

Printing Dynamic HTML Content

The first stage of this tutorial is to add code to the project to create some HTML content and send it to the Printing framework in the form of a print job.

Begin by locating the HTMLPrintActivity.java file (located in the Project tool window under app -> java -> ebookfrenzy.com -> htmlprint) and loading it into the editing panel. Once loaded, modify the code so that it reads as outlined in the following listing:

package com.ebookfrenzy.htmlprint;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.content.Context;

public class HTMLPrintActivity extends Activity {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html_print);

        WebView webView = new WebView(this);
        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view,
                                                    String url)
            {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                createWebPrintJob(view);
                myWebView = null;
            }
        });

        String htmlDocument =
           "<html><body>== Creating the Web Page Printing Example ==
<google>ADSDAQBOX_FLOW</google>
The second example application to be created in this chapter will provide the user with an Overflow menu option to print the web page currently displayed within a WebView instance. Create a new project in Android Studio, entering WebPrint into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.

On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 21: Android 5.0 (Lollipop). Continue to proceed through the screens, requesting the creation of a blank activity named WebPrintActivity with a corresponding layout named activity_web_print with the remaining properties set to the default values.