Make URL open in the same Activity

Asked

Viewed 4,491 times

6

I’m creating a WebView for Android. When I load the App, the imported page works perfectly, but when I click on some link page, that same link opens in another browser.

I wonder how I can make all the links open in the same Activity of that WebView?

Source code:

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

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

    LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);


        WebView wv = new WebView(this); //(WebView) findViewById(R.id.webView1);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        wv.setLayoutParams(lp);

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);

        wv.loadUrl("http://www.exemplo.com.br/");

        ll.addView(wv);
    }
}

1 answer

7


Set a Webviewclient in your Webview for it to handle Urls:

wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});
  • Thank you very much young man. It worked right! : D

  • Accept Eduardo’s answer as correct then...

Browser other questions tagged

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