0

can anybody tell me how to detect click events on html page i have one android app. in that app i have loaded one thrid party website. in that website some print buttons are there.But when i touch print button nothing is happened. So i just want to know that how can detect the button click on webpage and execute printjob

here is my main activity

package com.example.re;

public class MainActivity extends AppCompatActivity {
    public WebView webView;

    private WebView mWebView;





    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView)findViewById(R.id.web);

        webView.setWebViewClient(new overload());
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
                handler.proceed();
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favion){
                super.onPageStarted(view,url,favion);

            }
            @Override
            public void onPageFinished(final WebView view, String url) {

                //show webview
                findViewById(R.id.web).setVisibility(View.VISIBLE);




            }
        });
        webView.loadUrl(url);
        // webView.loadUrl("https://192.168.0.186:8443/");//wifi
        // webView.loadUrl("https://192.168.1.6:8443/");//Ethernet
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        WebSettings webSettings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setJavaScriptEnabled(true);
        webView.setLongClickable(true);
        webSettings.setLoadWithOverviewMode(true);




   }


    public class overload extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    /**--------------------------------------------print--------------------------------------------------**/
    private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(MainActivity.this);
    webView.setWebViewClient(new WebViewClient() {

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

    @Override
    public void onPageFinished(WebView view, String url) {
    Log.i(TAG, "page finished loading " + url);
    createWebPrintJob(view);
    mWebView = null;
    }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " +
    "testing, testing...</p></body></html>";
    webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

    // Keep a reference to WebView object until you pass the PrintDocumentAdapter
    // to the PrintManager
    mWebView = webView;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void createWebPrintJob(WebView webView) {

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) MainActivity.this
                .getSystemService(Context.PRINT_SERVICE);

        String jobName = getString(R.string.app_name) + " Document";

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);

        // Create a print job with name and adapter instance
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());

        // Save the job object for later status checking

        ArrayList<PrintJob> printJobs = new ArrayList<PrintJob>();

        printJobs.add(printJob);
    }

    /**--------------------------------------------print--------------------------------------------------**/






    @Override
    public void onBackPressed()
    {
        if (webView.canGoBack()){webView.goBack();}
        else{

            new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogStyle)
                    .setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            MainActivity.this.finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();
            // super.onBackPressed();
        }
    }



}
3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.