Loading PDF in webview using Google Docs gview - java

I am trying to use the Google Docs gview to load a pdf in my webview. It is loading the viewer but the pdf page itself is not shown. I have used this technique with other projects (other target url's) without any problem but I can not get the Google Docs Gview page to work with this. I use
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
HttpHelper httphelper = new HttpHelper();
String pdf = "https://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
String result = httphelper.GetPage(Uri.parse("https://docs.google.com/gview?embedded=true&url=" + pdf));
webview.loadDataWithBaseURL("https://docs.google.com", result, "text/html", "utf-8", "https://docs.google.com");
}
Any idea why this is not working with the gview page?
Thanks

mWebview.getSettings().setJavaScriptEnabled(true);
String strPdf="https://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
mWebview.loadUrl("https://docs.google.com/gview?embedded=true&url=" + strPdf);

Related

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

Get random Youtube video in webview by clicking button in Android studio

I want to make an app where the user can get random youtube videos in a webview by clicking a button.
I already can show a video in my webview with the url but I want to get any random video from youtube by clicking my button.
In my xml file I have a webview and a button
´´´
MainActivity.java
´´´
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = "https://www.youtube.com/watch?v=1uwvxTT5V5M";
String dataUrl =
"<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView showYoutubeVideo = (WebView) findViewById(R.id.zufallvideo);
showYoutubeVideo.setWebViewClient(new WebViewClient(){
public boolean shouldoverloadUrlLoading(WebView view, String url) {
return false;
}
});
showYoutubeVideo.getSettings().setJavaScriptEnabled(true);
showYoutubeVideo.loadData(dataUrl,"text/html", "utf-8");
}
}
TL;DR: it's complicated.
Until Youtube exposes an endpoint in their API to do this, you're gonna need to find a workaround.
Some would suggest to query a search with "IMG" followed by a random integer (ex. "IMG 22930"). The end result is quite surprising, since tons of videos are uploaded without renaming.
Here's an example of this workaround implementation in python: https://github.com/0x01h/random-youtube-video-generator

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

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);

Change element's visibility when javascript calls a function in a WebView

I'm building a simple android app based on a WebView of a website that I personally built, so I'm just making the mobile app for it that shows the mobile version of the website. The app has an adMob Ad at the top of the page that I want to hide in certain cases after the user logs in(for example because he has a different account type and ads can't be shown when he logs in). So I tried using addJavascriptInterface to my WebView and calling a function from javascript when the Ad needs to be hidden but I can't make it work. I'm new to android programming so it may be I'm just missing something fundamental but I couldn't find an answer anywhere on internet. Here's my code:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdView = (AdView) findViewById(R.id.adView);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.addJavascriptInterface(new WebAppInterface(this, mAdView), "Android");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
absoluteUrl = "myWebSiteUrl";
mWebView.loadUrl(absoluteUrl);
mWebView.setWebViewClient(new MyAppWebViewClient(this, mAdView));
}
}
WebAppInterface:
public class WebAppInterface {
Context mContext;
AdView mAdView;
/** Instantiate the interface and set the context */
WebAppInterface(Context c, AdView mAdView) {
this.mContext = c;
this.mAdView = mAdView;
}
/** Show a toast from the web page and hide the Ad */
#JavascriptInterface
public void hideAd(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
mAdView.setVisibility(View.GONE);
}
}
Javascript code in my page
//..Some variables here
var accountType = 1;
$(document).ready(function() {
if (accountType == 1) {
Android.hideAd('The Ad is now hidden');
}
//Other js functions
It works when in hideAd() I only show the toast, but when I add
mAdView.setVisibility(View.GONE);
The Ad at the top doesn't hide and all the javascript code in the page no longer works, please help me understand what I'm doing wrong.

can't replace string with image in android webview html

Tiker is a string which have name and value both + and - i want to change that with images...
bt using this on + and - will hide bt image don't show...
so help to find my solution.
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
tiker = tiker.replaceAll("\\+","<img src=drawable\"up.PNG\">");
tiker = tiker.replaceAll("\\-","<img src=drawable\"down.PNG\">");
String summary = "<html><body><MARQUEE>"+tiker+"</MARQUEE></body></html>";
mWebView.loadData(summary, "text/html", "utf-8");
You cannot access images in 'drawable' directory from a WebView. You must put the images in 'assets' directory and access them with 'file:///android_asset/...'

Categories