Webview opens browser

Asked

Viewed 1,860 times

2

I made an application via Webview on Androidstudio, everything quiet without errors but when I put the information to login in my webview opens the default browser, I would like it to open in Webview itself:

package com.sirseni.simpleandroidwebviewexample;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

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

        WebView myWebView = (WebView) findViewById(R.id.myWebView);
        myWebView.loadUrl("http://meulink.com/");
        myWebView.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    // Use When the user clicks a link from a web page in your WebView
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("http://meulink.com/")) {
                return false;

            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

1 answer

1


Change the line:

myWebView.setWebViewClient(new MyWebViewClient());

for:

myWebView.setWebViewClient(new MyWebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

Source.

  • It worked partner, now another question, on the scroll, I can not see some options below, the site gets fixed, would have a solution?

  • Hello @Evertongouveia, good that it worked! I don’t have an answer to your second question, but I believe that this link can help you. If not solve, I suggest creating a new question. Great hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.