Search code examples
androidwebviewexternal

Webview - open links in external apps and browsers / android


I am a beginner in making android applications. I have made a web application in HTML which i want to be able to use in my application that I am making in android studio. I managed to make a simple web view in android studio which makes my web application work fine when i test it on my device. The only problem is that the web view handles all the URL´s inside my web application. The web application consists of tabs that directs me to different pages when i click on them which is what i want. But I have contact-buttons and different links that i want to be "released" from the web view. Lets take the contact button as an example. I have a Galaxy note which I am using to test my apps. When i open my application on my phone i see my Web application and i can navigate around. When i click the contact button, the web view handles the link and gives me a "page could not load" instead of opening the mail application on my phone. I also have buttons with links that I want to be able to open in an external browser on my phone. I hope you understand my problem and I am sorry for my bad english.

This is some of my code for the web view.

Mainactivity.java

public class MainActivity extends ActionBarActivity {

WebView browser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    browser = (WebView) findViewById(R.id.wvwMain);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setLoadWithOverviewMode(true);
    browser.getSettings().setUseWideViewPort(true);

    browser.setWebViewClient(new ourViewClient());
    try {
        browser.loadUrl("http://WebAppURL");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

OurViewClient.java

public class ourViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);

    return true; 
 }    
}

Solution

  • Try this way implement your WebViewClient like

     private class VideoWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try{
                    System.out.println("url called:::" + url);
                    if (url.startsWith("tel:")) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                        startActivity(intent);
                    }  else if (url.startsWith("http:")
                            || url.startsWith("https:")) {
    
                         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
                         startActivity(intent);
    
                    }  else if (url.startsWith("mailto:")) {
    
                        MailTo mt=MailTo.parse(url);
    
                        send_email(mt.getTo());
    
                    }
                    else {
                        return false;
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
    
                return true;
            }
    
        }
    

    and create send mail function like

       public void send_email(String email_add) {
        System.out.println("Email address::::" + email_add);
    
        final Intent emailIntent = new Intent(
                android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] { email_add });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
        yourActivity.this.startActivity(
                Intent.createChooser(emailIntent, "Send mail..."));
    
    }