3

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; 
 }    
}

2 Answers 2

6

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..."));

}
Sign up to request clarification or add additional context in comments.

5 Comments

I get Error:(24, 17) cannot find symbol method startActivity(android.content.Intent)and Error:(58, 9) cannot find symbol method getActivity()
The error disappeared but what about Error:(24, 17) cannot find symbol method startActivity(android.content.Intent)
@Stumpp i told you try this way MainActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
@Stumpp better to post your code. I want to see how you integrate?
I made it work, but It seems like every link opens in external browser whether or not it contains http. I have these tabs in my web app that i want to open in the webview but now it opens in external. How do i fix this?
1

My Code Working Good, just Remember the code line Sequence and placement.. Copy and paste code in your mainactivity.java file

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

    mywebView = findViewById(R.id.myview);
    WebSettings webSettings = mywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebView.loadUrl("http://yourwebsitename.com");
    mywebView.setWebViewClient(new MyCustomWebViewClient());

}

class MyCustomWebViewClient extends WebViewClient {

    @Override
    public  boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("yourwebsitename.com")) {
            //open url contents in webview
            return false;
        } else {
            //here open external links in external browser or app
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

    }
}
}

Comments

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.