Android webview launches browser when calling loadurl
Solution 1
Answering my question based on the suggestions from Maudicus and Hit.
Check the WebView tutorial here. Just implement the web client and set it before loadUrl. The simplest way is:
myWebView.setWebViewClient(new WebViewClient());
For more advanced processing for the web content, consider the ChromeClient.
Solution 2
Use this:
lWebView.setWebViewClient(new WebViewClient());
Solution 3
use like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dedline);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
}
Solution 4
Make your Activity like this.
public class MainActivity extends Activity {
WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.wvwMain);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
// Load the webpage
browser.loadUrl("http://google.com/");
}
}
Solution 5
I was facing the same problem and I found the solution Android's official Documentation about WebView
Here is my onCreateView()
method and here i used two methods to open the urls
Method 1 is opening url in Browser and
Method 2 is opening url in your desired WebView.
And I am using Method 2 for my Application and this is my code:
public class MainActivity extends Activity {
private WebView myWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
/* Method : 1
This following line is working fine BUT when we click the menu item then it opens the URL in BROWSER not in WebView */
//((WebView) rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);
// Method : 2
myWebView = (WebView) rootView.findViewById(R.id.detail_area); // get your WebView form your xml file
myWebView.setWebViewClient(new WebViewClient()); // set the WebViewClient
myWebView.loadUrl(mItem.url); // Load your desired url
}
return rootView;
} }
Ray
Having been doing mobile app for a while. Started on dumb phones back in 1998. Then on so called "smart phones" since 2005. Went through J2ME, Symbian, Blackberry. All of those are dead or dead man walking thanks to iOS, and Android. Glad I hopped on the new boats in 2010.
Updated on July 08, 2022Comments
-
Ray almost 2 years
I created an
Activity
that has a title and a web view in aLinearLayout
. In theonResume()
method it callswebView.loadUrl(url)
. The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. What I want to see is the page being shown in theWebView
below the title. What could be the problem?Edit: Ok, did some further search and found this one:
Clicking URLs opens default browser
It points to the
WebView
tutorial here.Just implement the web client and set it.
-
Magno C about 10 yearsTo avoid WebView to launch the default browser when open initial page.
-
Josh over 8 yearsThanks for this unbelievable awesome little piece of witchcraft! +1 ...MINUS ONE FOR YOU, Android...
-
Omid1989 over 6 yearsExcellent answer. Solved my problem too.
-
logicbloke over 6 yearsDoes this only work for the initial loading? What if you want to navigate the browser from within the app?
-
Thomas Pritchard about 6 years
myWebView.webViewClient = WebViewClient()
in Kotlin! -
apex39 over 5 years
setJavaScriptEnabled(true)
introduces XSS vulnerabilities into your app. Do not use it if you do not need JavaScript -
Jordan H about 5 yearsThe line enabling JavaScript is super important otherwise you'll get
You need to enable JavaScript to run this app.
and googling it will only reveal people having issues with react native and you'll go crazy because JavaScript is already enabled in the browser settings. -
Vladyslav Matviienko almost 5 years@DigvijaySingh check the original of accepted answer. Originally it didn't have this code. It was added way after this answer
-
Abduhafiz over 3 yearsWini, you can do smth. like this
@ Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (mWebView.canGoBack()) { mWebView.goBack(); } else { finish(); } return true; } } return super.onKeyDown(keyCode, event); }
-
Sujit almost 3 yearsDamn, it didn't even occur to me to use both. I use
onPageFinished()
fromWebViewClient
andonConsoleMessage()
fromWebChromeClient
. Thanks! -
Jaimin Modi over 2 yearsIts still opens in browser with end / not correct answer..
-
Kevin Worth over 2 yearsActually, I'm finding this same behavior. Not sure how to explain it, but this is what I'm seeing as well.
-
qkx almost 2 yearsOh, classic Googlers. They always forget to mention something very important in their android documentation....It is maddening, 80% times when I copy paste their code I have to google "why XYZ does not work" and find an answer on stackoverflow...How hard is it to not half-ass documentation Google?