Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package com.example.teacher.mywebview
  2.  
  3. import android.content.Context
  4. import android.support.v7.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.print.PrintAttributes
  7. import android.print.PrintManager
  8. import android.view.Menu
  9. import android.view.MenuItem
  10. import android.webkit.WebResourceRequest
  11. import android.webkit.WebView
  12. import android.webkit.WebViewClient
  13. import kotlinx.android.synthetic.main.activity_main.*
  14.  
  15. class MainActivity : AppCompatActivity() {
  16. //declare of web view object
  17. private var myWebView:WebView? = null
  18. //simple html page for testing
  19. val htmlDocument = "<html><body><h1>Ivgany Print Page Test</h1><hr>"+
  20. "<p>this is a sample page to print ivgany test</p></body></html>"
  21.  
  22. override fun onCreate(savedInstanceState: Bundle?) {
  23. super.onCreate(savedInstanceState)
  24. setContentView(R.layout.activity_main)
  25. //printWebView()
  26. //setSupportActionBar(toolBar)
  27. configureWebPage()
  28.  
  29. }
  30.  
  31. private fun configureWebPage() {
  32. myWebView?.webViewClient = object : WebViewClient() {
  33. override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
  34. return super.shouldOverrideUrlLoading(view, request)
  35. }
  36. }
  37. myWebView?.settings?.javaScriptEnabled = true
  38. //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
  39. webView.loadUrl("https://www.hackeru.co.il/course/applications")
  40. }
  41.  
  42. private fun printWebView() {
  43. val webView = WebView(this)
  44. webView.webViewClient = object :WebViewClient(){
  45. //not allow to override while the page is loading, don't print before we ready
  46. override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
  47. return false
  48. }
  49.  
  50. override fun onPageFinished(view: WebView?, url: String?) {
  51. createWebPringJob(view)
  52. myWebView=null
  53. }
  54. }
  55.  
  56.  
  57.  
  58. //load the html data into the webview
  59. //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
  60. webView.loadUrl("https://www.hackeru.co.il/course/applications")
  61. myWebView=webView
  62.  
  63. }
  64.  
  65. private fun createWebPringJob(view: WebView?) {
  66. //getting the print manager service
  67. val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager
  68. //getting the print adapter
  69. val printAdapter = webView.createPrintDocumentAdapter("myDocument")
  70. //create a job name
  71. val jobName = getString(R.string.app_name) + " Print Test"
  72. //all is ready, you can printa the paga
  73. printManager.print(jobName,printAdapter,PrintAttributes.Builder().build())
  74. }
  75.  
  76.  
  77. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  78. if (item.itemId == R.id.action_print)
  79. {
  80. createWebPringJob(myWebView)
  81. }
  82. return super.onOptionsItemSelected(item)
  83. }
  84.  
  85. override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  86. menuInflater.inflate(R.menu.menu_web_print,menu)
  87. return true
  88. }
  89. }
  90.  
  91.  
  92.  
  93. <?xml version="1.0" encoding="utf-8"?>
  94. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
  95. <item android:id="@+id/action_print"
  96. android:orderInCategory="100"
  97. app:showAsAction="never"
  98. android:title="@string/print_string"/>
  99. </menu>
  100.  
  101.  
  102. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  103. android:orientation="vertical"
  104. android:layout_width="match_parent"
  105. android:layout_height="match_parent">
  106.  
  107. <WebView
  108. android:layout_width="match_parent"
  109. android:layout_height="match_parent"
  110. android:id="@+id/webView"/>
  111.  
  112. </LinearLayout>
  113.  
  114.  
  115. <resources>
  116. <string name="app_name">Print Ivgany Sister</string>
  117. <string name="action_settings">Settings</string>
  118. <string name="print_string">Print</string>
  119. </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement