Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:03:01+00:00 2026-06-03T15:03:01+00:00

So I need the user clicked link in webview, in this case it is

  • 0

So I need the user clicked link in webview, in this case it is a link containing a .pdf file. I have code that launches the PDF reader but it doesn’t get the link so it just loads the PDF reader on click of a PDF file. How do I intercept this link and feed it to my PDF reader intent?

Code:

public class atcFaa extends Activity {
WebView webview;
private String url;
ProgressBar pd = null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.atccti);

    pd = (ProgressBar) findViewById(R.id.web_view_progress_bar);

    webview = (WebView) findViewById(R.id.ctiWebView);
    webview.getSettings().setJavaScriptEnabled(true);

    Button openPdfBtn = new Button(this);
    webview.addJavascriptInterface(openPdfBtn, "openPdfBtn");
    openPdfBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            openPdf();
        }
    });

    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            if (progress < 100 && pd.getVisibility() == ProgressBar.GONE) {
                pd.setVisibility(ProgressBar.VISIBLE);
            }
            pd.setProgress(progress);
            if (progress == 100) {
                pd.setVisibility(ProgressBar.GONE);
            }
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            webview.getSettings().setJavaScriptEnabled(true);
            // do your handling codes here, which url is the requested url
            // probably you need to open that url rather than redirect:
            if (url.startsWith("tel:")) {
                startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
            } else if (url.startsWith("mailto:")) {
                url = url.replaceFirst("mailto:", "");
                url = url.trim();
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL,
                        new String[] { url });
                startActivity(i);

            } else if (url.startsWith("geo:")) {
                try {
                } catch (Exception e) {
                    System.out.println(e);
                }

            } else if (url.endsWith("pdf")) {
                try {

                }

                catch (ActivityNotFoundException e) {
                    Toast.makeText(atcFaa.this, "No PDF Viewer Installed",
                            Toast.LENGTH_LONG).show();
                }
            }

            else {
                view.loadUrl(url);
            }
            return true;
            // then it is not handled by default action
        }

    });

    webview.loadUrl("http://www.faa.gov/air_traffic/publications/");
}

protected void openPdf() {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.parse(url);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.atcAbout2:
        Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG)
                .show();
        break;
    case R.id.atcContact2:
        emailme();
        break;

    }
    return true;
}

private void emailme() {
    // TODO Auto-generated method stub
    String domsEmail = "MYEMAIL@EXAMPLE.com";
    String message = "Insert Message Here";
    String myemail[] = { domsEmail };
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, myemail);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "ATC Assistant");
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(emailIntent);
}

/*
 * @Override public void onBackPressed() { if (webview.canGoBack())
 * webview.goBack(); else super.onBackPressed(); }
 */

public void setUrl(String url) {
    this.url = url;
}

public String getUrl() {
    return url;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:03:03+00:00Added an answer on June 3, 2026 at 3:03 pm

    I don’t know the structure of your html code, but I’ll pretend you have a button. Here the code to have in your html:

    <button onclick='openPdfBtn.performClick();'>Open pdf</button> 
    

    and then add a js interface to your webview:

    Button openPdfBtn = new Button(this); //this is not gonna be visible
    yourWebView.addJavascriptInterface(openPdfBtn, "openPdfBtn"); //here you bind the js with the native button
    openPdfBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            openPdf();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code for extract files dir and title. The user download when clicked the
I have two PHP files that I need to link. How can I link
Use case: User clicks a link, a modal window is displayed containing one big
I need to find out the word in an image where user has clicked.
My point is this. For test i need when user check chk1 the chk2
I have a simple table view which is editable. All I need the user
I have a server with php4 and pho5 support. But i need to user
I need to isolate user-written code snippets from each other in javascript. For now
I have this peice of coding which simply swaps an image when a link
I have a general create function that submits a new user to the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.