badmanthe5
badmanthe5

Reputation: 71

webview opens default browser, I don't know where to put code

So, I've been Googleing around for 4 hours now :( and i cant get the website to only stay opened in the app. After you type in the username and password upon clicking log in the page opens in the default browser. What I learned is, I should use: shouldOverrideUrlLoading() but I don't know where place it and how to use it to. This is my MainActivity.java, where or what do I put in there to stop it from opening in the default browser.(ignore the *)

package com.example.***.*******c;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.*******.org/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Upvotes: 3

Views: 6704

Answers (1)

James McCracken
James McCracken

Reputation: 15766

You have to implement a class that extends WebViewClient and override it in there. Try this:

public class MainActivity extends Activity {

    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());
        mWebView.loadUrl("http://www.*******.org/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class MyWebViewClient extends WebViewClient {

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

Upvotes: 14

Related Questions