Home » Android » webview.loadUrl() problem in android?

webview.loadUrl() problem in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am loading a URL whose datatype is String in a WebView. Using something like:

webview.loadUrl(string_variable);

but it is automatically redirected to the browser.

I got this in my LOGCAT,

02-13 14:11:08.586: INFO/ActivityManager(5Cool: Displayed activity com.example.brown/.Bru_Press_MostRecent_ArticleView: 2583 ms (total 2583 ms)
02-13 14:11:09.376: INFO/ActivityManager(5Cool: Starting activity: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=http://mobile.twitter.com/brownuniversity/statuses/8681812931 cmp=com.android.browser/.BrowserActivity (has extras) }
02-13 14:11:09.506: INFO/ActivityManager(5Cool: Start proc com.android.browser for activity com.android.browser/.BrowserActivity: pid=285 uid=10014 gids={3003, 1015}
How to&Answers:

Does the same thing happen with a different URL? I’m wondering if mobile.twitter.com has a redirect which is causing the Browser to be launched.

Answer:

You can remedy this by overriding your WebView’s WebViewClient. All it takes is something like this:

    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           return super.shouldOverrideUrlLoading(view, url);
        }
    });

Just be aware that WebView is more limited than full blown browser

Answer:

you need add setting for app
Follow:

mWebView.getSettings()
.setUserAgentString(
“Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3”);