Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.teacher.mywebview
- import android.content.Context
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.print.PrintAttributes
- import android.print.PrintManager
- import android.view.Menu
- import android.view.MenuItem
- import android.webkit.WebResourceRequest
- import android.webkit.WebView
- import android.webkit.WebViewClient
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- //declare of web view object
- private var myWebView:WebView? = null
- //simple html page for testing
- val htmlDocument = "<html><body><h1>Ivgany Print Page Test</h1><hr>"+
- "<p>this is a sample page to print ivgany test</p></body></html>"
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //printWebView()
- //setSupportActionBar(toolBar)
- configureWebPage()
- }
- private fun configureWebPage() {
- myWebView?.webViewClient = object : WebViewClient() {
- override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
- return super.shouldOverrideUrlLoading(view, request)
- }
- }
- myWebView?.settings?.javaScriptEnabled = true
- //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
- webView.loadUrl("https://www.hackeru.co.il/course/applications")
- }
- private fun printWebView() {
- val webView = WebView(this)
- webView.webViewClient = object :WebViewClient(){
- //not allow to override while the page is loading, don't print before we ready
- override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
- return false
- }
- override fun onPageFinished(view: WebView?, url: String?) {
- createWebPringJob(view)
- myWebView=null
- }
- }
- //load the html data into the webview
- //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
- webView.loadUrl("https://www.hackeru.co.il/course/applications")
- myWebView=webView
- }
- private fun createWebPringJob(view: WebView?) {
- //getting the print manager service
- val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager
- //getting the print adapter
- val printAdapter = webView.createPrintDocumentAdapter("myDocument")
- //create a job name
- val jobName = getString(R.string.app_name) + " Print Test"
- //all is ready, you can printa the paga
- printManager.print(jobName,printAdapter,PrintAttributes.Builder().build())
- }
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
- if (item.itemId == R.id.action_print)
- {
- createWebPringJob(myWebView)
- }
- return super.onOptionsItemSelected(item)
- }
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
- menuInflater.inflate(R.menu.menu_web_print,menu)
- return true
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
- <item android:id="@+id/action_print"
- android:orderInCategory="100"
- app:showAsAction="never"
- android:title="@string/print_string"/>
- </menu>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <WebView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/webView"/>
- </LinearLayout>
- <resources>
- <string name="app_name">Print Ivgany Sister</string>
- <string name="action_settings">Settings</string>
- <string name="print_string">Print</string>
- </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement