Why iframe works in webview on Android 4.0+ ICS? - android

I load a Google Docs' iframe into a webview in Android, this iframe is loading a PPT file (Power Point file) from a external server of Google Docs. This is the Android Code:
public class Test_iframeActivity extends Activity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
//String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
String customHtml = "<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' width='100%' height='100%' style='border: none;'></iframe>";
webView.loadData(customHtml, "text/html", "UTF-8");
}}
My question is... why I can see the iframe works fine into Android 4.0 but I can't see it into Android 2.1 to 2.3? (I don't know if in Android 3 works).

SOLVED!
The mistake was the ASCII encoding... I shoud replace the #, %, /, and ? by other characters, please check http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String) for more information.

Related

Displaying docx, excel, pdf files within my app

I have an Android app where I want to display files in pdf, excel, docx, txt formats from their respective Urls. I am able to display these files using external apps, but for this, the file is first downloaded and stored on the device and then displayed. I do not want the downloading, I tried using Google document viewer to display the files in a WebView, but since the documents are private, they wont display. I have tried something like below :-
mDocumentUrl = mIntent.getStringExtra(Constant.DOCUMENT_URL);
wvDocumentReader.getSettings().setJavaScriptEnabled(true);
wvDocumentReader.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
wvProgressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
wvProgressBar.setVisibility(View.GONE);
}
});
wvDocumentReader.loadUrl("http://docs.google.com/gview?embedded=true&url="+ mDocumentUrl);
Can anyone suggest a library or a workaround to achieve this? Any help is appreciated.
I think you should use custom library for getting that done .
See : https://github.com/googlesamples/android-PdfRendererBasic
There is also another way for displaying PDF without calling another application
This is a way for showing PDF in android app that is embed PDF document to android webview using support from http://docs.google.com/viewer
pseudo
String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+'
width='100%' height='100%'
style='border: none;'></iframe>";
a sample is is shown below
String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'
width='100%' height='100%'
style='border: none;'></iframe>";
Code
WebView wv = (WebView)findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(doc);
//wv.loadData( doc, "text/html", "UTF-8");
and in manifest provide
<uses-permission android:name="android.permission.INTERNET"/>
OR
See this :https://stackoverflow.com/a/43292083/9976418

WebView is not using Cookies for loading images

I have troubles when I want my WebView to load images that requires Cookies.
I have set my cookies on the 'CookieManager'
final android.webkit.CookieManager instance = android.webkit.CookieManager.getInstance();
instance.setAcceptCookie(true);
instance.setCookie(".example.fr", mCookies, new ValueCallback<Boolean>() {
#Override
public void onReceiveValue(final Boolean value) {
loadWebView();
}
});
The WebView is then loaded with a custom HTML string because the app is generating the proper HTML.
private void loadWebView() {
// this string is an example of a generated HTML
String htmlContent =
"<!DOCTYPE html>" +
"<html><head>" +
"<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\" media=\"screen, projection\"/></head>" +
"<body><img src=\"www.example.fr/img.jpg\"/></body></html>";
mWebView.loadDataWithBaseUrl("file:///android_asset/", htmlContent, "text/html", "UTF-8", null);
}
I tried to proxy the network calls with Charles Proxy and I noticed that the request to www.example.fr/img.jpg had no cookie set in the headers. But, when I inspect the WebView using Chrome debbuging, I can see that the Cookies are properly under the Resources tab.
It seems that they are not used for the image downloading.
Any hints or advices to make the WebView using Cookies for resource downloading ?
Thank you.
I've faced with same problem, it is related with following changes:
https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView
to fix it you need set:
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);

How to draw Hindi text on Webview in android?

I am new in android so please help me. my question is
I want to draw hindi text on WebView in android, I am using following code
<string name="hindi_content">विज्ञान के इस युग में मानव को जहां कुछ वरदान मिले है </string>
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String s = getResources().getString(R.string.hindi_content);
wv = (WebView)findViewById(R.id.webView);
wv.loadData(s, "text/html", null);
}
but it displaying the special characters. so please help me.
Use the following to load हिन्दी:
wv.loadData(s, "text/html; charset=utf-8", "utf-8");
This happens because of a bug with the encoding parameter of loadData in most Android versions. This parameter is ignored for some reason so the UTF-8 based hindi characters will not be rendered. To fix this you can use one of the following alternatives.
webview1.loadData(hindi_content, "text/html; charset=UTF-8", null);
webview1.loadDataWithBaseURL(null, hindi_content, "text/html", "utf-8", null);

white screen in webview

Have blank white screen when using Webview. I see many topics with this problem but no one solved. So i tried another one...
It's my first time project on android.
Ok i optimised my code but its still show me gray screen on emulator and white screen on device. Someine help me.
public class Video extends Activity{
private WebView video;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
video = (WebView)findViewById(R.id.webshow);
video.getSettings().setJavaScriptEnabled(true);
video.getSettings().setPluginState(WebSettings.PluginState.ON);
/*video.getSettings().setPluginsEnabled(true);*/
String html = "http://www.twitch.tv/wingsofdeath";
final String temp = "<div class=\"live_site_player_container swf_container js-player-container\" data-channel=\"wingsofdeath\" id=\"standard_holder\" style=\"width: 100%; height: 211px;\">\n" +
"<object type=\"application/x-shockwave-flash\" name=\"live_site_player_flash\" data=\"http://www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf\" width=\"100%\" height=\"100%\" id=\"live_site_player_flash\" style=\"visibility: visible;\"><param name=\"allowNetworking\" value=\"all\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"allowScriptAccess\" value=\"always\"><param name=\"wmode\" value=\"opaque\"><param name=\"flashvars\" value=\"team=solomid&auto_play=true&hide_chat=true&publisherGuard=null&publisherTimezoneOffset=420&channel=wingsofdeath&hostname=www.twitch.tv&loadCallback=Twitch.player._twitchPlayerLoaded&backgroundImageUrl=http://static-cdn.jtvnw.net/jtv_user_pictures/anonymous_monkey-320x240.jpg\"></object>\n" +
"</div>";
video.loadDataWithBaseURL (html, temp, "application/x-shockwave-flash", "UTF-8","");
}}
what i doing wrong?? Someone talk about using webchromeclent. pls explain me about that.
(Manifest and another.. is right)
Thank you.
Can i get info about "file///android_asset" its local html file as i stand... how i can load my data there if it's possible??

Why Android WebView does not display properly HTML page with Hebrew text when text-align set to "justify"

I want to display local html file on a WebView. (Android 2.3.3)
The HTML contains Hebrew text. I want the text to be justified, so in my css file I do the following:
body
{
text-align: justify; direction: rtl;
}
But for some reason the text end up being messed up:
And this is definitely not "justified" but more aligned to the left.
Any idea how can overcome this problem?
It's working perfectly fine on any other browser than the WebView. (Including WebKit based ones)
This code worked for me:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
String webtext = "שלום";
String summary = "<html lang=\"he\"><body><p dir=\"rtl\">" + webtext + "</p></body></html>";
myWebView.loadData(summary, "text/html", null);
}

Categories

Resources