3

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;
    }
}
VendettaDroid
  • 3,086
  • 2
  • 26
  • 40
badmanthe5
  • 71
  • 1
  • 2
  • 9
  • Possible duplicate of [Android webview launches browser when calling loadurl](http://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl) – rds Jan 21 '16 at 12:18

1 Answers1

15

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;
        }
    }
}
James McCracken
  • 14,210
  • 4
  • 49
  • 59
  • This is what i did now but it still opens up the default browser thus exiting the app: – badmanthe5 Sep 28 '12 at 21:32
  • 1
    `shouldOverrideUrlLoading(...)` should return `false` to prevent the default browser from being opened. – Squonk Sep 28 '12 at 21:33
  • Thank You it works now! I learned something over the past few days. How do i add something that will save the username and password by going into the setting and choosing such an option. Also how do i add an Exit app option upon clicking the options button on the phone. Youtube tutorials or links are fine. I wan to learn now. Again thank you so much guys. 2 days of work paid off finally. – badmanthe5 Sep 28 '12 at 21:40
  • 1
    Thanks, James, your answer really worked. I just deleted the onCreateOptionsMenu function. I tried so many answers on so many forums and none worked except this one. – Adrian Cumpanasu Nov 04 '13 at 20:35