How can I open multiple PDF Links in Android WebView in Android? - java

I'm building android app where, I placed links to the page in webview on which multiple links are there pointing to PDFs. Everyday, new links are updated. I want to make such functionality that when user clicks on the particular PDF link, it should get opened in default PDF viewer or there itself.
Right now, nothing is happening when user clicks on PDF link within webview.
WebView w=(WebView)findViewById(R.id.web1);
WebSettings webSettings=w.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
w.setWebViewClient(new WebViewClient());
w.loadUrl("http://collegecirculars.unipune.ac.in/sites/examdocs/_layouts/mobile/view.aspx?List=7ed6607e-6c43-401a-a922-bf8d8bf08ed8&View=dc261157-c533-4a60-977b-506fd87b2a19");

You can use Google Docs Viewer to read your pdf online:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://collegecirculars.unipune.ac.in/sites/examdocs/Examiantions%20Timetables/Master-In-ARTS(M.A)(Rev.2008)-(24908)_Spl4-8-16.pdf?Mobile=1&Source=%2Fsites%2Fexamdocs%2F_layouts%2Fmobile%2Fview%2Easpx%3FList%3D7ed6607e%252D6c43%252D401a%252Da922%252Dbf8d8bf08ed8%26View%3Ddc261157%252Dc533%252D4a60%252D977b%252D506fd87b2a19%26ViewMode%3DSimple%26CurrentPage%3D1";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
open in browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
To override current URL :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// here you can check PDF
url = url.toLowerCase();
view.loadUrl(url);
return true;
}
}
set this MyWebViewClient to your webView.
PaymentWebViewClient paymentWebViewClient = new PaymentWebViewClient();
webView.setWebViewClient(paymentWebViewClient);

Related

webview onPageFinished does not detect the selection of a video from the home page on YouTube mobile version

I entered this code under onPageFinished:
edittext1.setText(webview1.getUrl());
When I google YouTube and i select YouTube, edittext changes its name, But when I select a video on the home page in YouTube page, edittext does not change the name
Why don't you try this code? :
binding.webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
binding.urlEditText.setText(url); // set the url
view.loadUrl(url);
return false;
}
});

How to open new intent when webview go out of my website

I'm making an Android App with Java in Android Studio for my website using webview, but i've a lot of links which go out of website. How can i detect if link redirect from www.mywebsite.com to www.anotherwebsite.com and open it in another page intent?
You can use the code below.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
and create and inner class
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
You can find complete documentation here

webview never call ShuldOverrideUrlloading in webviewclient in android

I have a webview and a link on the URL that I have loaded on that webview, I want such like that if user clicks in this link the app will go to the background and open that link with the default web browser of that device. To do this I have done the following:
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.getSettings().setPluginState(PluginState.ON);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null
&& url.startsWith("http://the6figuremarketer.com/apps/androidfb21/thks.html")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
web.loadUrl("http://the6figuremarketer.com/apps/androidfb21/index.html");
but I figured out that the shouldoverrideurl method is nevet get called. what should I do now? can anyone help?
Look at shouldOverrideUrlLoading in WebView for Android not running.
shouldOverrideUrlLoading() is not called when you call loadurl on webview. It is called only when you click on a link in webview.

WebView only opens some URLs but not all

When I make a call to my WebView and give it a URL I want it to open within the app (i.e. on my page) but it doesn't always do this.
Some pages will open within my app but others will open in the default android web browser.
Here's my code. Any help on this would be much appreciated.
WebView webView = ((WebView)findViewById(R.id.website));
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(website);
To override the default behavior, use something like:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
More or less duplicate of the existing question Clicking URLs opens default browser?

Creating An In-Game Web Browser on Android

In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".

Categories