Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- menu
- =============
- <?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="فاتورة طباعة"
- />
- </menu>
- layout
- ===========
- <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>
- java
- ============
- package com.example.nadininvoice
- import android.content.Context
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.print.PrintAttributes
- import android.print.PrintManager
- import android.util.Log
- import android.view.Menu
- import android.view.MenuItem
- import android.webkit.WebResourceRequest
- import android.webkit.WebView
- import android.webkit.WebViewClient
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- //declare of web object
- private var myWebView: WebView?=null
- //simple html page for testing
- val htmlDocument = "<html><body><h1>Nadin Wedding bus cost</h1><hr>" +
- "<p>Bus for friends from hacker U - 3000Nis</p>" +
- "<p>Bus for groom familiy (Hassan) - 7500nis</p>" +
- "<p>Bus for groom friends (Hassan) - 7500nis</p>" +
- "<hr><b>total cost : 18000Nis</b>" +
- "</body></html>"
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- printWebView()
- Toast.makeText(this,"loading site...",Toast.LENGTH_LONG).show()
- }
- private fun printWebView()
- {
- val webView = WebView(applicationContext)
- webView.webViewClient = object : WebViewClient(){
- override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
- return false
- }
- override fun onPageFinished(view: WebView?, url: String?) {
- createWebPrintJob(view)
- myWebView=null
- }
- }
- //load the html data into the webView
- //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
- //load the html data into the webView by a url
- webView.loadUrl(("https://www.hackeru.co.il/course/applications"))
- myWebView=webView
- }
- private fun createWebPrintJob(view: WebView?){
- //getting the print service
- val printManger = getSystemService(Context.PRINT_SERVICE) as PrintManager
- //get the print adapter
- val printAdapter = view!!.createPrintDocumentAdapter("myDoucment")
- // create a job name
- val jobName = getString(R.string.app_name)+ " print test"
- //all is ready, you can print the document, create a print command
- printManger.print(jobName,printAdapter,PrintAttributes.Builder().build())
- }
- override fun onOptionsItemSelected(item: MenuItem?): Boolean {
- /*
- if (item.itemId == R.id.action_print)
- {
- createWebPrintJob(myWebView)
- }
- */
- when (item!!.itemId){
- R.menu.menu->{
- createWebPrintJob(myWebView)
- }
- else->{
- }
- }
- return super.onOptionsItemSelected(item)
- }
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
- menuInflater.inflate(R.menu.menu,menu)
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement