Loading pdf or url in Android Webview - android

I am working on an Android application that opens urls its content is HTML or Pdf ... I used this way to open it
WebView webView = (WebView) mContentView.findViewById(R.id.web_view);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
// This is the url that may be .html or .pdf
String url = "https://uva.onlinejudge.org/external/1/119.html";
//String url = "https://uva.onlinejudge.org/external/11/1124.html";
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress >= 100) {
setContentShown(true);
}
}
});
webView.loadUrl(url);
the first url opens normally in the webview, but the second url doesn't show anything in the webview .. and If I try to open it on the browser it redirects me to this url [https://uva.onlinejudge.org/external/11/p1124.pdf] and open normally.
Any help will be appreciated.

Set a download listener to your webview. Inside the listener, you can choose to download the PDF by overriding its methods.
webView.setDownloadListener(new (DownloadListener listener) { // is it a pdf? });

Related

Webview navigation returning wrong url

I have a webview that looks like this
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("https://myurl.com/");
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(LoginActivityWebView.this, url, Toast.LENGTH_SHORT).show();
return false;
}
}
When i change my URL to google everything works great, i can navigate to different sites and everything works.
When I put in my URl it is supposed to link to myurl2, but when I click it the toast prints myurl instead and the page juts reloads (not helpful!).
Any idea as to how this could happen and where shouldOverrideUrlLoading gets its url from?
If i load myurl in a browser and the user clicks a button then it works correctly.
If i load myurl in a webview, but open a browser on links pressed, then when the link is pressed it opens myurl on in a broswer and if you click the button again it correctly opens myurl2 (super weird).
Any ideas?
If the URL is opening in Chrome properly, you should consider adding, webView.getSettings().setJavaScriptEnabled(true) to your code and try again.
You can get the loaded URL from
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

Unable to disable autoplay for video from a URL in WebView

I have webview displayed on custom dialog, I am loading an URL to WebView which is video URL, by default the video is loaded and starts playing, I want to disable auto play function. Here is my code for WebView
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setJavaScriptEnabled(true);
wvActiveVideo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wvActiveVideo.getSettings().setPluginState(WebSettings.PluginState.ON);
wvActiveVideo.getSettings().setMediaPlaybackRequiresUserGesture(false);
wvActiveVideo.setWebChromeClient(new WebChromeClient());
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setSupportZoom(true);
wvActiveVideo.getSettings().setBuiltInZoomControls(true);
wvActiveVideo.getSettings().setDisplayZoomControls(false);
String url = model.getVideo();
wvActiveVideo.loadUrl(model.getVideo());
It depends of your algorithm.
First advice , before load url show your user play button background.
Second advice is data manupulation ;
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById(\"video-test\").innerHTML = \"\";void(0);");
webView.setVisibility(View.VISIBLE);
}

How to open PDF from URL without Google doc or Google drive

I am new in android.
I am trying to open PDF from url in webview. Its working fine for small PDF but if i receive large PDF from server, Then it give error,
(No Preview Available).
I want to show pdf from server only in view mode and user should not download and also need to clear cache after back press. Below are the code which i am using to open the PDF from server.
enter image description here
Code:
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new Callback());
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfUrl);
use this
private String showPdf(String url) {
String googleDocsUrl = "http://docs.google.com/viewer?embedded=true&url=";
return googleDocsUrl+Uri.encode(url);
}
if its still not available, it maybe the url is on private server.
void showWebView(){
webview.setWebViewClient(new MyBrowser());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setSupportZoom(true);
webview.loadUrl("https://docs.google.com/viewer?url=" + your_url+ "&embedded=true");
}
private class MyBrowser extends WebViewClient {
#Override
public void onUnhandledKeyEvent(WebView view, KeyEvent ke) {
Log.e("Unhandled Key Event", ke.toString());
//mProg.cancel();
}
#Override
public void onPageFinished(WebView view, String url) {
//mProg.cancel();
super.onPageFinished(view, url);
wv_webview_content.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('ndfHFb-c4YZDc-GSQQnc-LgbsSe ndfHFb-c4YZDc-to915-LgbsSe VIpgJd-TzA9Ye-eEGnhe ndfHFb-c4YZDc-LgbsSe')[0].style.display='none'; })()");
}
}
try this
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=https://sherlock-holm.es/stories/pdf/a4/1-sided/advs.pdf");
replace this url https://sherlock-holm.es/stories/pdf/a4/1-sided/advs.pdf with your url. if still not work then check your pdf.

why my webview is opend in a own browser

I have a android code like this:
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://test.com");
the problem is, when I start the app, it is opend in a new browser intend and not in my webView of the app.
how to avoid this?
Add a WebViewClient. This will prevent the default browser to open the URL (or get a selection dialog when there is no default)
myWebView.setWebViewClient(new WebViewClient());
More information can be found on Android website: http://developer.android.com/guide/webapps/webview.html
With the WebViewClient you can do more things, like preventing to load an URL, or change the URL.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Blank empty page in android webview after facebook login using javascript

i am developing a new android application,and using a webview to view my website in one the website pages there is a link for login with facebook
after logging directly, it returns a blank and embty webview
i have seen couple of topics here regarding the same issue but it is not exactly like mine
please note that if i am using normal desktop browser the issue is not there
I assume that there is a property of the webview should be adjusted , below my webview code
webView1=(WebView)findViewById(R.id.webView1);
webView1.setWebViewClient(new MyWebViewClient());
WebView webView1 = (WebView) findViewById(R.id.webView1);
webView1.setWebChromeClient(new WebChromeClient());
webView1.getSettings().setAppCacheEnabled(true);
webView1.getSettings().setDatabaseEnabled(true);
webView1.getSettings().setDomStorageEnabled(true);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView1.getSettings().setGeolocationEnabled(true);
webView1.loadUrl("http://www.myactivepoint.com/mobile/maplp.asp");
i really approciate your support
I had a similar experience with you. I solved the problem by the following methods.
public class CustomWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
if(url.startsWith("https://m.facebook.com/dialog/oauth")){
String redirectUrl = "http://www.mydomain.com/myReturnUrl";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}}
and just add it to your WebView
webview.setWebViewClient(new CustomWebViewClient());
This is my solution. After successfull login you have to redirect the page. You can get the address from refsrc parameter in the Facebook URL, but sometimes (e.g. next try to log in) refsrc is missing so I use domain parameter. You can override the URL address if you need. See the code:
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(final WebView view, final String url)
{
if(getActivity()!=null)
{
if(url.contains("facebook.com") && url.contains("dialog/oauth") &&
(url.contains("&refsrc") || url.contains("&domain")))
{
Uri uri = Uri.parse(url);
String callbackUrl = uri.getQueryParameter("refsrc");
if(callbackUrl==null)
callbackUrl = "http://" + uri.getQueryParameter("domain");
view.loadUrl(callbackUrl); // callback URL
}
}
}
}

Categories

Resources