Android +1 button in WebView - android

I've tried putting Google's +1 button in WebView using the methods they describe. I've initialized the WebView as follows:
final WebView web = (WebView)findViewById(R.id.webView);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setSavePassword(false);
web.getSettings().setBuiltInZoomControls(false);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setHorizontalScrollBarEnabled(false);
web.setBackgroundColor(0xff2e2e2e);
web.loadDataWithBaseURL(null, htmlCodeGoesHere, "text/html", "utf-8", null);
And the html code:
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone href="http://stackoverflow.com"></g:plusone>
The problem is... the button doesn't display at all.
How do I fix it? By the way - I also want the button to launch a new window instead using the WebView. Is there a simple solution?
Thanks

The problem lies in permissions system in WebView. Scripts in local files have problems accessing external resources. The solution is to make WebView think local code was loaded from external website.
web.loadDataWithBaseURL("http://fake.com", htmlCodeGoesHere, "text/html", "utf-8", null);
The button will appear, but unfortunetely it doesn't work well in WebView.

I am not too experienced with WebView, but the fact the the button doesn't show up at all, sounds like it could be an issue in your layout/main.xml file. Have you taken a look at this yet?
Also, for the button to launch a new window, I think it is possible to attach an setOnClickListener, once that is done just treat it as a button, and open a new window. I hope that is possible.

Related

why is (WebView) in brackets in android

My background is mainly web design using JS HTML and CSS writing simple apps - nothing professional. I have recent had the urge to learn a bit about Android development. I quickly came across the below line. I understand defining the variable as a webview etc, but I don't understand why '(WebView)', in brackets, is used in the statement.
WebView browser;
browser=(WebView)findViewById(R.id.webkit);
I can only imagine its something to do with android as I haven't come across it in JS before. Any help or info on where I can read up on this would be much appreciated!
It is type casting.
WebView browser;
In the above line, you are declaring browser as an object of WebView.
So it can hold only WebView
So, in the below line, you are setting the type to WebView in order to prevent error if the findViewById(R.id.webkit); return otherwise
browser=(WebView)findViewById(R.id.webkit);
This is actually a Java feature.
You can read more about it in Oracle Java Documentation
That is what we call as type casting!
Code:
WebView browser;
browser = (WebView) findViewById(R.id.webkit);
Explanation:
You are assigning value to the instance of WebView(i.e., browser) by typecasting the View from your design(.xml file) to WebView. Because, .java file should recognize what view is the webKit in the design.

window.reload in Webview

I'm trying to get location.reload() to work in a webView. But I can not load it with a url. I have to load it like this:
web_view.loadData(filterString(editor.cleanText), "text/html", "UTF-8")
But then the js code dosn't seeem to be able to relaod the webView. filterString removes comments because it says Unexpected end of input" otherwise
How do I make this work?
Yes I had the same problem sometimes this won't work:
window.open('[YOUR_HTML_PAGE]');
by example
window.open('index.html');
So I use this and this should normally work.
window.open('[YOUR_HTML_PAGE]', '_self', false);
by example
window.open('index.html', '_self', false);
Try window.location.href=page to access. In my case, running fine!

How do I get my android webview make a load locally

I have a doubt. What happens is the following:
I exported my game by using cocoonjs contruct 2, but I want to run it on my android webview by, because I want to implement starApp, and cocoonjs not support this.
So, using my code below, I get only a blank screen.
But in my launcher cocoonjs, this same project works normally, just that I select Canvas + can anyone help me?
web = (WebView) findViewById(R.id.webInicial);
web.setWebChromeClient(new WebChromeClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/www/index.html");
webView.loadData(yourData, "text/html", "UTF-8");
Where youData is the actual HTML data.
Have you tried getApplicationContext().getAssets().open("www/index.html");?
If that doesn't work, I'd say try the solutions in this post and let us know if it worked for you.

Issue with WebView and Google Maps

In my application on click of a button i am trying to launch the following url using a web view :
https://maps.google.com/maps?saddr=indiranagar bangalore&daddr=mgroad bangalore
My code is as below:
WebView webView=new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("https://maps.google.com/maps?saddr=indiranagar bangalore&daddr=mgroad bangalore");
When the web view appears it first displays the following:
And immediately displays the following:
I do not get to see the maps. Am i missing on something in my code. Can someone kindly help me with this please. Thanks in advance.
I had the same issue in using Google Maps URL into a ColorBox popup.
I found out that you can add &output=embed at the end of the URL to make it show in an iframe.
Maybe this solution will help you too.
So try:
webView.loadUrl("https://maps.google.com/maps?saddr=indiranagar bangalore&daddr=mgroad bangalore&output=embed");
i found that WebView does not support javascript by default, and this will lead google map jump to a URL that contains a params "nojs",
so, just add this line for your webview, problem gone.
webview.getSettings().setJavaScriptEnabled(true);

WebView Problem on Youtube site in Android?

i tried to load the url ww.youtube.com on my app in a webview. but it cant be load completely. it loads just like below image. in the browser it loads comfortably. why? Any Idea?
image http://www.freeimagehosting.net/uploads/d7356dd8e1.png
the simple answer is to call the youtube app thats loaded on every phone with your webview. check out the code on http://fluxkore.com/index.php/android-apps/ to call the youtube app.....
Enable JavaScript! =)
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.youtube.com");
It might be a problem with WebViews - WebViews aren't fully fledged browsers, and have limited functionality. For example, the reference page specifically says that WebViews don't handle JavaScripts. If JavaScripts, Flash or something like that is required to properly load YouTube, then that could be why the WebView doesn't handle it properly.

Categories

Resources