I am trying to add an WebView to my app, but would like to allow to open inside my app only links which are connected with my domain (www.example.com). All other links which doesn't contain the name of my domain (example.com) should be opened externaly by default browser. Using information from stockoverflow I have prepared the following code, but it still doesn't work and all links are opened inside my app, despite shouldOverrideUrlLoading is created ("if (Uri.parse(url).getHost().contains("example.com"))"). Is anyone able to help to resolve this problem? I was trying a lot of samples, but still cannot find the way how to fix it, but I fell that it can be just a small mistake in the code.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Resolved! I am not sure that the code is 100% correct, but works fine and maybe it will be useful for someone (I am not the author, I just used a few samples from stackoverflow and put them together):
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.uk");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Related
I have created a WebView of this website https://smartfix.pk/ but in my app, WhatsApp is not working,
when I click it gives me the error
webpage at whatsapp://send/?phone=923005475032&text=Hello%2C%20I%20have%20a%20question%20about%20SmartFix%20Services could not be loaded because: net::ERR_UNKNOWN_URL_SCHEME
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new Callback());
web.loadUrl("https://smartfix.pk/");
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
}
}
try to add this, i'm not sure it's will work for that page but when i'm using webview i always enable all to be sure
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}
I recently build an android app using webview in Android Studio but it doesn't open/download the pdf files from the website. Is there a way to let users open pdf files from a web page without adding them to the app. I want to update the pdf files on the website without having to change the app.
This is my code now:
package com.something.app;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("something");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
If you want to open the PDF in device in-built browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
Or instead in web view:
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
Try using pdfviewer for that. It's much better than using webview.
The link to the github is this: https://github.com/barteksc/AndroidPdfViewer
This is more useful because it's easy to use.
All you need is this code:
build gradle (app):
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
main.java
pdfView = (PdfView) findViewById(R.id.pdfview);
Uri a = new Uri("*your URL*");
pdfView.fromUri(a).load();
Android weview app I compiled in andriod studio crashes immediately with a blackscreen when I open it. The app uses an iframe embed url.Compiles fine but crashes with a black screen when I open it.
Why is it crashing?
public class MainActivity extends AppCompatActivity {
private WebView zwebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView = (WebView) findViewById(R.id.myWebView);
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
I just copy-paste your code and its working fine the video is loading in the webview so if you face issue can you kindly share the log details that what error you actually getting.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView zwebView = new WebView(this);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
setContentView(zwebView);
}
}
Here is the code which I use and it's working. not crashing after all.
I'm doing a website to Android app,but .aspx page to webview is not loading, I got white screen.
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webview);
// Enable Javascript
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new myWebViewClient());
String url = "https://something.aspx";
wv.loadUrl(url);
}
}
myWebViewClient
class myWebViewClient extends WebViewClient {
}
}
How to load .aspx page in android WebView please solve my issue,
Thanks in advance.
Add the code below in your myWebViewClient class:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Is possible remove the banners in a webview? In my webview i display a forum and there is an annoying banner i want to remove.. Is it possible? Is there a way like adBlock or something like that? My code:
public class MainActivity extends Activity
{
protected WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null)
{
webview.loadUrl("http://www.proboards.com/");
}
}
#Override
protected void onSavedInstanceState(Bundle outState) {
//super.onSavedInstanceState(outState);
webview.saveState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks.
If you want to hide a banner in a specific page, look at the page's source for the banner's id and inject some javascript code (example here) in order to set its visibility to hidden.