summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSvetoslav Ganov <svetoslavganov@google.com>2013-10-30 21:04:48 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-10-30 21:04:49 +0000
commit79efac6b3aedb6e5bcb54bec7566fa9038240f93 (patch)
treef9563589875fa58d7ad3d0b235c03bd2bae3d759
parent1c254de3dd54f1f9817e79f51577b3e40515f4bd (diff)
parent42c6250c9db71d1a1d0c36c560954831fc1dacbc (diff)
downloadandroid_development-79efac6b3aedb6e5bcb54bec7566fa9038240f93.tar.gz
android_development-79efac6b3aedb6e5bcb54bec7566fa9038240f93.tar.bz2
android_development-79efac6b3aedb6e5bcb54bec7566fa9038240f93.zip
Merge "Add print samples to the ApiDemos app." into klp-dev
-rw-r--r--samples/ApiDemos/Android.mk2
-rw-r--r--samples/ApiDemos/AndroidManifest.xml27
-rw-r--r--samples/ApiDemos/res/layout/print_bitmap.xml23
-rw-r--r--samples/ApiDemos/res/layout/print_html_from_screen.xml22
-rw-r--r--samples/ApiDemos/res/layout/print_html_off_screen.xml24
-rw-r--r--samples/ApiDemos/res/raw/android_logo.pngbin0 -> 10722 bytes
-rw-r--r--samples/ApiDemos/res/raw/motogp_stats.html333
-rw-r--r--samples/ApiDemos/res/values/strings.xml6
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/PrintBitmap.java82
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/PrintCustomContent.java4
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlFromScreen.java93
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java89
12 files changed, 703 insertions, 2 deletions
diff --git a/samples/ApiDemos/Android.mk b/samples/ApiDemos/Android.mk
index 066c497cb..ca48f581d 100644
--- a/samples/ApiDemos/Android.mk
+++ b/samples/ApiDemos/Android.mk
@@ -12,6 +12,8 @@ LOCAL_SRC_FILES += \
LOCAL_JAVA_LIBRARIES := telephony-common mms-common
+LOCAL_STATIC_JAVA_LIBRARIES = android-support-v4
+
LOCAL_PACKAGE_NAME := ApiDemos
LOCAL_SDK_VERSION := current
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index ff7db377d..d7bda3125 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -996,6 +996,33 @@
</intent-filter>
</activity>
+ <activity android:name=".app.PrintBitmap"
+ android:label="@string/print_bitmap"
+ android:enabled="@bool/atLeastKitKat">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".app.PrintHtmlFromScreen"
+ android:label="@string/print_html_from_screen"
+ android:enabled="@bool/atLeastKitKat">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".app.PrintHtmlOffScreen"
+ android:label="@string/print_html_off_screen"
+ android:enabled="@bool/atLeastKitKat">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
<activity android:name=".app.PrintCustomContent"
android:label="@string/print_custom_content"
android:enabled="@bool/atLeastKitKat">
diff --git a/samples/ApiDemos/res/layout/print_bitmap.xml b/samples/ApiDemos/res/layout/print_bitmap.xml
new file mode 100644
index 000000000..cce522acb
--- /dev/null
+++ b/samples/ApiDemos/res/layout/print_bitmap.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/image"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:src="@raw/android_logo"
+ android:contentDescription="@string/android_logo">
+</ImageView>
diff --git a/samples/ApiDemos/res/layout/print_html_from_screen.xml b/samples/ApiDemos/res/layout/print_html_from_screen.xml
new file mode 100644
index 000000000..535bcb2d1
--- /dev/null
+++ b/samples/ApiDemos/res/layout/print_html_from_screen.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<WebView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/web_view"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:padding="16dip">
+</WebView>
diff --git a/samples/ApiDemos/res/layout/print_html_off_screen.xml b/samples/ApiDemos/res/layout/print_html_off_screen.xml
new file mode 100644
index 000000000..799b07927
--- /dev/null
+++ b/samples/ApiDemos/res/layout/print_html_off_screen.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textIsSelectable="false"
+ android:text="@string/print_html_off_screen_msg">
+</TextView>
diff --git a/samples/ApiDemos/res/raw/android_logo.png b/samples/ApiDemos/res/raw/android_logo.png
new file mode 100644
index 000000000..f970c262e
--- /dev/null
+++ b/samples/ApiDemos/res/raw/android_logo.png
Binary files differ
diff --git a/samples/ApiDemos/res/raw/motogp_stats.html b/samples/ApiDemos/res/raw/motogp_stats.html
new file mode 100644
index 000000000..b55ed9eb3
--- /dev/null
+++ b/samples/ApiDemos/res/raw/motogp_stats.html
@@ -0,0 +1,333 @@
+<!DOCTYPE html>
+<html>
+<body>
+<table>
+<caption>500cc/MotoGP champions</caption>
+<thead><tr>
+<th scope="col">Season</td>
+<th scope="col">Rider</td>
+<th scope="col">Constructor</td>
+</tr></thead><tbody>
+<tr>
+<td>2012</td>
+<td>Jorge Lorenzo</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2011</td>
+<td>Casey Stoner</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>2010</td>
+<td>Jorge Lorenzo</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2009</td>
+<td>Valentino Rossi</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2008</td>
+<td>Valentino Rossi</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2007</td>
+<td>Casey Stoner</td>
+<td>Ducati</td>
+</tr>
+<tr>
+<td>2006</td>
+<td>Nicky Hayden</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>2005</td>
+<td>Valentino Rossi</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2004</td>
+<td>Valentino Rossi</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>2003</td>
+<td>Valentino Rossi</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>2002</td>
+<td>Valentino Rossi</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>2001</td>
+<td>Valentino Rossi</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>2000</td>
+<td>Kenny Roberts, Jr.</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1999</td>
+<td>Ëlex CrivillŽ</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1998</td>
+<td>Michael Doohan</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1997</td>
+<td>Michael Doohan</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1996</td>
+<td>Michael Doohan</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1995</td>
+<td>Michael Doohan</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1994</td>
+<td>Michael Doohan</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1993</td>
+<td>Kevin Schwantz</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1992</td>
+<td>Wayne Rainey</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1991</td>
+<td>Wayne Rainey</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1990</td>
+<td>Wayne Rainey</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1989</td>
+<td>Eddie Lawson</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1988</td>
+<td>Eddie Lawson</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1987</td>
+<td>Wayne Gardner</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1986</td>
+<td>Eddie Lawson</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1985</td>
+<td>Freddie Spencer</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1984</td>
+<td>Eddie Lawson</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1983</td>
+<td>Freddie Spencer</td>
+<td>Honda</td>
+</tr>
+<tr>
+<td>1982</td>
+<td>Franco Uncini</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1981</td>
+<td>Marco Lucchinelli</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1980</td>
+<td>Kenny Roberts</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1979</td>
+<td>Kenny Roberts</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1978</td>
+<td>Kenny Roberts</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1977</td>
+<td>Barry Sheene</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1976</td>
+<td>Barry Sheene</td>
+<td>Suzuki</td>
+</tr>
+<tr>
+<td>1975</td>
+<td>Giacomo Agostini</td>
+<td>Yamaha</td>
+</tr>
+<tr>
+<td>1974</td>
+<td>Phil Read</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1973</td>
+<td>Phil Read</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1972</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1971</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1970</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1969</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1968</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1967</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1966</td>
+<td>Giacomo Agostini</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1965</td>
+<td>Mike Hailwood</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1964</td>
+<td>Mike Hailwood</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1963</td>
+<td>Mike Hailwood</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1962</td>
+<td>Mike Hailwood</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1961</td>
+<td>Gary Hocking</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1960</td>
+<td>John Surtees</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1959</td>
+<td>John Surtees</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1958</td>
+<td>John Surtees</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1957</td>
+<td>Libero Liberati</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1956</td>
+<td>John Surtees</td>
+<td>MV Agusta</td>
+</tr>
+<tr>
+<td>1955</td>
+<td>Geoff Duke</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1954</td>
+<td>Geoff Duke</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1953</td>
+<td>Geoff Duke</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1952</td>
+<td>Umberto Masetti</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1951</td>
+<td>Geoff Duke</td>
+<td>Norton</td>
+</tr>
+<tr>
+<td>1950</td>
+<td>Umberto Masetti</td>
+<td>Gilera</td>
+</tr>
+<tr>
+<td>1949</td>
+<td>Leslie Graham</td>
+<td>AJS</td>
+</tr>
+</tbody><tfoot></tfoot></table>
+</body>
+</html> \ No newline at end of file
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index b9502e534..804087dd5 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -875,8 +875,14 @@
<!-- app/print print examples strings -->
<!-- ================================= -->
+ <string name="print_bitmap">App/Print/Print Bitmap</string>
+ <string name="print_html_from_screen">App/Print/Print HTML from screen</string>
+ <string name="print_html_off_screen">App/Print/Print HTML off screen</string>
<string name="print_custom_content">App/Print/Print Custom Layout</string>
<string name="print">Print</string>
+ <string name="print_html_off_screen_msg">From the overflow menu you can print some
+ off screen content.</string>
+ <string name="android_logo">Android logo</string>
<!-- ============================ -->
<!-- graphics examples strings -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PrintBitmap.java b/samples/ApiDemos/src/com/example/android/apis/app/PrintBitmap.java
new file mode 100644
index 000000000..762478a99
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PrintBitmap.java
@@ -0,0 +1,82 @@
+package com.example.android.apis.app;
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
+import android.os.Bundle;
+import android.print.PrintManager;
+import android.support.v4.print.PrintHelper;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.webkit.WebView;
+import android.widget.ImageView;
+
+import com.example.android.apis.R;
+
+/**
+ * This class demonstrates how to implement bitmap printing.
+ * <p>
+ * This activity shows an image and offers a print option in the overflow
+ * menu. When the user chooses to print a helper class from the support
+ * library is used to print the image.
+ * </p>
+ *
+ * @see PrintManager
+ * @see WebView
+ */
+public class PrintBitmap extends Activity {
+
+ private ImageView mImageView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.print_bitmap);
+ mImageView = (ImageView) findViewById(R.id.image);
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+ getMenuInflater().inflate(R.menu.print_custom_content, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.menu_print) {
+ print();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ private void print() {
+ // Get the print manager.
+ PrintHelper printHelper = new PrintHelper(this);
+
+ // Set the desired scale mode.
+ printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
+
+ // Get the bitmap for the ImageView's drawable.
+ Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
+
+ // Print the bitmap.
+ printHelper.printBitmap("Print Bitmap", bitmap);
+ }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PrintCustomContent.java b/samples/ApiDemos/src/com/example/android/apis/app/PrintCustomContent.java
index 0a7715c25..7a7e7ed80 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/PrintCustomContent.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PrintCustomContent.java
@@ -376,9 +376,9 @@ public class PrintCustomContent extends ListActivity {
// Add the height but if the view crosses the page
// boundary we will put it to the next one.
- pageContentHeight += view.getHeight();
+ pageContentHeight += view.getMeasuredHeight();
if (currentPage < 0 || pageContentHeight > mRenderPageHeight) {
- pageContentHeight = view.getHeight();
+ pageContentHeight = view.getMeasuredHeight();
currentPage++;
// Done with the current page - finish it.
if (page != null) {
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlFromScreen.java b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlFromScreen.java
new file mode 100644
index 000000000..55c98ffc9
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlFromScreen.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.print.PrintManager;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import com.example.android.apis.R;
+
+/**
+ * This class demonstrates how to implement HTML content printing
+ * from a {@link WebView} which is shown on the screen.
+ * <p>
+ * This activity shows a simple HTML content in a {@link WebView}
+ * and allows the user to print that content via an action in the
+ * action bar. The shown {@link WebView} is doing the printing.
+ * </p>
+ *
+ * @see PrintManager
+ * @see WebView
+ */
+public class PrintHtmlFromScreen extends Activity {
+
+ private WebView mWebView;
+
+ private boolean mDataLoaded;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.print_html_from_screen);
+ mWebView = (WebView) findViewById(R.id.web_view);
+
+ // Important: Only enable the print option after the page is loaded.
+ mWebView.setWebViewClient(new WebViewClient() {
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ // Data loaded, so now we want to show the print option.
+ mDataLoaded = true;
+ invalidateOptionsMenu();
+ }
+ });
+
+ // Load an HTML page.
+ mWebView.loadUrl("file:///android_res/raw/motogp_stats.html");
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+ if (mDataLoaded) {
+ getMenuInflater().inflate(R.menu.print_custom_content, menu);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.menu_print) {
+ print();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ private void print() {
+ // Get the print manager.
+ PrintManager printManager = (PrintManager) getSystemService(
+ Context.PRINT_SERVICE);
+ // Pass in the ViewView's document adapter.
+ printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), null);
+ }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java
new file mode 100644
index 000000000..3625784cd
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.print.PrintManager;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import com.example.android.apis.R;
+
+/**
+ * This class demonstrates how to implement HTML content printing
+ * from a {@link WebView} which is not shown on the screen.
+ * <p>
+ * This activity shows a text prompt and when the user chooses the
+ * print option from the overflow menu an HTML page with content that
+ * is not on the screen is printed via an off-screen {@link WebView}.
+ * </p>
+ *
+ * @see PrintManager
+ * @see WebView
+ */
+public class PrintHtmlOffScreen extends Activity {
+
+ private WebView mWebView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.print_html_off_screen);
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+ getMenuInflater().inflate(R.menu.print_custom_content, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.menu_print) {
+ print();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ private void print() {
+ // Create a WebView and hold on to it as the printing will start when
+ // load completes and we do not want the WbeView to be garbage collected.
+ mWebView = new WebView(this);
+
+ // Important: Only after the page is loaded we will do the print.
+ mWebView.setWebViewClient(new WebViewClient() {
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ // Get the print manager.
+ PrintManager printManager = (PrintManager) getSystemService(
+ Context.PRINT_SERVICE);
+ // Pass in the ViewView's document adapter.
+ printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(),
+ null);
+ }
+ });
+
+ // Load an HTML page.
+ mWebView.loadUrl("file:///android_res/raw/motogp_stats.html");
+ }
+}