Android WebView click open with in WebView not a default browser?
Try:
WebView view =(WebView)findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Source: This answer by @momo on very similar question: Link should be open in same web view in Android . Hope this helps.
Android WebView click open within WebView not a default browser
You have to set up a webViewClient for your webView.
Sample:
this.mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Clicking URLs opens default browser
If you're using a WebView
you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView
using a WebViewClient
. The method you want is shouldOverrideUrlLoading()
. This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient
of your WebView
using the setWebViewClient()
method.
If you look at the WebView
sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Android Webview - How I can open external links in default browser?
You have to send an Intent to to the default web browser, in order to open the link in the browser, so maybe something like this:
Create a class that extends WebViewClient, in this way:
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("https://mywebsite.domain.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
} else {
view.loadUrl(url);
}
return true;
}
}
In your MainActivity, in the onCreate method, set the MyCustomWebViewClient before the call to loadUrl method, like this:
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyCustomWebViewClient());
myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
}
// the rest of your code
Android webview opens page in default browser instead of my webview
You need to implement setWebViewClient(....)
like so.
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Update:
Make your Activity like so
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://news.google.com/");
}
}
Android WebView opening links in default browser - Open links in app
If you override shouldOverrideUrlLoading(...)
in your MyAppWebViewClient
and return false
, your webview should automatically attempt to load the url. The documentation states the following:
Return: True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
see shouldOverrideUrlLoading(...) documentation.
mWebView.setWebViewClient(new MyAppWebViewClient(){
@Override
boolean shouldOverrideUrlLoading (WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
Related Topics
Clear Recent Apps Wipe the Apps Memory and My Receiver Stopped Working
Javadoc in Eclipse Failing to Recognize Packages
Android: Failed to Allocate Memory
Android Emulator Camera Custom Image
Actionbarsherlock - How to Set the Padding of Each Actionbar's Icon
Classloader Referenced Unknown Path: /Data/App/
Comparing Two Times in Android
How to Draw Border on Just One Side of a Linear Layout
What Is the Correct Way to Specify Dimensions in Dip from Java Code
How to Monitor Sim State Change
Android Studio Fails to Build New Project, Timed Out While Wating for Slave Aapt Process
Adb Got Two Same Serial Numbers When Connected to Two Smart Phones
Alertdialog.Getbutton() Method Gives Null Pointer Exception Android
Android Sms Receiver Not Working
Apache Http Connection with Android 6.0 (Marshmallow)