why my webview displays differently this link than my browser - android

I'm tring to load this url in a WebView :
https://crocodoc.com/view/PFk2xRsJYPjfDhhm-2nfzi1CnZtUvvXqpy7Uh7uCcFGTfiFDbVpzbgJ2kSUyGGRkf1M_eq_COHKAf14Okz0ShL0WzoNiprqf2UWXUSP7hJ0CHiCGaP4Vs7CHKJAW2A
I tried the basic way :
wv.loadUrl("..");
within an Iframe :
String str2 = "<body style=\"margin:0px;padding:0px;overflow:hidden\">\n" +
"<iframe src=\""+"https://crocodoc.com/view/"+sessionId+"\" frameborder=\"0\" style=\"overflow:hidden;height:100%;width:100%\" height=\"100%\" width=\"100%\"></iframe>\n" +
"</body>";
wv.loadData(str2, "text/html","utf-8");
With a classic client, a chrome chient, enabling more of less every WebView Setting that I could think of. But I loose the controls, I just see an horribele zommed view of the document, I can't zoom etc...
I only left :
wv.getSettings().setJavaScriptEnabled(true);
In my phone browsers It displays as I want it to.
So my question is : why ? how should I do to make it look the same way than in firefox or chrome.
Thanks

Related

Is it possible to affect mobile browser contents with an Android app?

I'm starting to build an Android app with my limited knowledge and I'm not sure if this functionality is possible, but hopefully the great minds at SO can help though!
The basic functionality of the app would be similar to the user entering a text string such as "Chicago Bulls" into the app and then when they're using their mobile browser, all instances of that text string on a page are formatted into bold font and red letters for example.
Is this possible with an Android app at all? Or something similar?
I Googled for building Android apps integrating with mobile browsers, but I wasn't able to find anything relevant. I thought that potentially I could build a mobile Chrome extension, but it looks like Chrome Extensions aren't supported by Android yet?
I am not clear your question. I assume that you want to change some font within webview? If so then you can try
Example:
a) Init you webview
webview = (WebView) findViewById(R.id.webView);
b) Create you won stylesheet and attach to html body
String pish = "<html><head><style type=\"text/css\">#font-face {font-family:
MyFont;src: url(\"file:///android_asset/font/BMitra.ttf\")}body {font-family:
MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String pas = "</body></html>";
c) Load on webview
String webContent = pish + YourTxext + pas;
String internalFilePath = "file://" + getFilesDir().getAbsolutePath() + "/";
webview.loadDataWithBaseURL(internalFilePath, webContent, "text/html", "UTF-8", null);

Control a youtube video in WebView Programmatically

I am loading a youtube video in a webview and it works fine, what i wan't is to be able to control the video (i.e start , stop etc.) programmatically.
I tried a few things and it didn't work
String playVideo= "<html><body><iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/QKm-SOOMC4c?enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></body></html>";
myWebView = (WebView) findViewById( R.id.Webview );
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.loadData(playVideo, "text/html", "utf-8");
myWebView.loadUrl("javascript:playVideo()");
You can look at how I'm doing it in this library: android-youtube-player.
You can start by looking at the WebView class.
The idea of calling webview.loadUrl("javascript:playVideo()"); is correct. But you need to wait for the IFrame library to be loaded and ready.
AFAIK, developer can't control that.
Even if developer control that by finding html element by getElementById method and click it, it'll be blocked if Google change the html structure.
I recommend the way is being provided by Google officially.
https://developers.google.com/youtube/android/player/

Web view problems

I have some problem in my web view.
I want to load a string from a WordPress blog, witch have html tags such as <a> and image tag and ... .
So my problems are:
As I mention above, I want to load a local string and I want to handle user click on the links, so I load data like this into the web view:
WebView webview = (WebView) this.findViewById(R.id.mainWV);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
String s="<p>It will enable Seattle-based Alaska to expand into lucrative hubs such</p>\n<p><img class=\"aligncenter size-full wp-image-1035\" src=\"http://ichef.bbci.co.uk/news/660/cpsprodpb/D09F/production/_89070435_89069565.jpg\" alt=\"\" width=\"300\" height=\"120\" /></p>\n<p>as San Francisco and Los Angeles.</p>\n";
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
and another way I tried was:
String head1 = "<head></head>";
String text = "<html>" + head1
+ "<body dir=\"rtl\" >" + s
+ "</body></html>";
webview.loadData(text, "text/html", "utf-8");
and my client is :
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("USER_CLICKED", url + "USER_CLICKED");
return true;
}
}
Ok, now when I run the app, and when I click on <a> I never see 'USER_CLICKED', but the webview content change and it seems web view is empty, I mean is white as snow.
notice 1: when I try this:
webview.loadUrl("https://android-arsenal.com/");
and run the app, when I click on the links in the loaded web view, every thing is OK, and i see this Log: 'USER_CLICKED' and the related URL.
notice 2: yes i try a lots of different URL, but loading from string, nothings change in click handling.
notice 3: I test in android 5.1 and 4.1 in 4.1 clicked recognized and is see 'User.. but in the 5.1 the white page story happens.(Edit: android 6 also do not show 'USER... ')
my number 2 problem is, when I call this:
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
the image tag does not load! I mean it is just ignore to load the images, and I do not know why.
notice 3: when I copy text from inside the web view there is some rectangle in the text.
OK every one, after a long time, i find the problem, so as i mentioned above, i take string from WordPress rest API(json), so in my word, the string should be OK, but as i find out, there is some extra '\' in the string, and the string is like:
<p>It will enable Seattle....
, as simple it is i just use:
s=s.replaceAll("\\","");
so, thank any one who see this post.
A URL is a URL. The first item is HTML, not a URL. If you want to load the URL then you have to pass a valid URL (not HTML) and do not expect the URL to be magically parsed out of the HTML string that you are loading. In fact the first item is not even valid HTML, it is a part of HTML, potentially a snippet, but it's not even enclosed in the HTML tags that I expect a web view would need.

Does webview take HTML tag <object>?

I tried many times and try to use HTML tag <object>, but app just crash and disappear.
Each time when I use normal html tag such as <html>, <body>, <h1>, <div>, <td>, <tr> etc. Those can be shown directly without any error.
This time I try to embed an <object> tag to webview and it always crash!
Does anyone who know how to do this directly inside code? OR, webview just cannot do this?
here is my code.
String html =
"<html>" +
"<body><h1>This is a test !!</h1>" +
"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='200' height='200'></object>" +
"<param name='quality' value='high'>" +
"<param name='allownetworking' value='internal'>" +
"<param name='allowScriptAccess' value='never'>" +
"<param name='movie' value='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1'>" +
"<param name='allowFullScreen' value='true'>" +
"<embed src='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1' width='200' height='200' quality='high' allownetworking='internal' allowscriptaccess='never' allowfullscreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed>" +
"</object>" +
"</body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
You have one <object> tag and two </object> tags. The first </object> has got to go.
The embed tag is for browsers that don't recognize the object tag. Those browsers will see the embed tag and will try to use it. Browsers that support the object tag will use the object and ignore the embed, as long as the embed is within the object. If the embed is outside the object, object aware browsers may load both the object and embed tags.
WebView can definitely handle the html tag. I don't think you should use loadDataWithBaseURL though, try this instead:
mWebView.loadData(html, "text/html", "UTF-8");
I'm not too sure your <object> will work though...
Here's a better way to play a YouTube video if that's all you need:
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=<video-id>")));
Put your video id in place of the <video-id>

Is there any way to have WebView auto-link URLs and phone numbers in Android?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?
With a TextView you would simply set the android:autoLink to the desired settings:
<TextView
android:autoLink="web|phone"
... />
but I can't find any equivalent for WebView.
If you are loading your own (web) content from a String, then you can do something like this:
final String content = "My email is: firstname#email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:
yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");
This will inject javaScript into the already loaded web page.
There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.
You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Categories

Resources