Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - why webview redirect to webbrowser

i try a simple webView appication. and this is my code :

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView)findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://eepis-its.edu");
}

public class myWebClient extends WebViewClient{
    public void onPageStarted(WebView view, String url, Bitmap favicon){
        super.onPageStarted(view, url, favicon);
    }

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

public boolean onKeyDown(int keyCode, KeyEvent event){
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()){
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

and, when i try to click hyperlink on that site, i redirected to mobile browser. why its happen? what i wanted is, when i click hyperlink its show on webview too.

please help me.

like image 310
DevYudh Avatar asked Dec 16 '22 04:12

DevYudh


1 Answers

You have to override the standard behavior, which launches the browser when links are clicked. Use a WebViewClient's shouldOverrideUrlLoading(). There is an example of this in the SDK (copied below for convenience).

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

...then you just call someWebView.setWebViewClient(new HelloWebViewClient());.

like image 199
Chris Cashwell Avatar answered Dec 31 '22 13:12

Chris Cashwell