final String youtube = "<iframe class='youtube-player' type='text/html' width='200px' height='200px' src='http://www.youtube.com/embed/7vcc68CY9Rw' frameborder='0'>";
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
dialog.show();
return true;
}
public void onPageFinished(WebView view, String url) {
if(dialog.isShowing()) {
dialog.dismiss();
}
}
});
webview.loadData(youtube,"text/html", "utf-8");
I don't know why youtube video doesn't play here, maybe I missed write something? The webview load the data but when I press play button nothing happened there. what is the problem here?
Why not just launch an intent to play the video and then let the user decide if they want their preferred browser or the YouTube app to play the video? When the user presses back they will return to your app. (note this may not work on emulators)
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Just download source from here and check it.It works
http://code.google.com/p/html5webview/source/checkout
put this in your webView client
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// YouTube video link
if (url.startsWith("vnd.youtube:"))
{
int n = url.indexOf("?");
if (n > 0)
{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length(),n)));
}
return (true);
}
return (false);
}
it is better to start an intent with action view to view you tube videos..
Related
I want to play a music from any music app(gaana/wynk/saavn etc) using webview android. My requirement is I want to open a music app in webview and user can play music directly using webview just like we open (gaaan/wynk/saavn) in web browser of mobile device and you can play any song.
I tried gaana and wynk music, both are able to open on webview but when i am clicking on any songs that songs does not play. do i need to enable any settings for that i already have enabled javascript.
below is the code i have tried.
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
Please suggest any solution for this.
its because app cant use storage.
WebView webView = (WebView) findViewById(R.id.webview);
webview.getSettings().setDomStorageEnabled(true);
This solves the problem !
I created an android app with webview.
Within the web page I have amazon banners.
If a user clicks on that banner I would like it to be open in the native android browser and not in webview.
I've tried several methods after searching and trying with no success.
When the banner is clicked - the app still shows the amazon site in webview mode.
Can anyone help me fix this issue ?
Your help is greatly appreciated...
Below is the code I use :
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
view.loadUrl(url);
loadAd();
return true;
}
});
webView.loadUrl("http://starter_url");
loadAd();
The function shouldOverrideUrlLoading() should:
Returns True if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
So you have to change your code to something like:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});
I am showing PDF files by using google docs in WebView in android.
How to remove or hide "Sign In" button? I have attached screenshot below. Thanks in advance.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?url=http://www.ex.com/terms.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Add the embedded=true parameter.
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=https://orimi.com/pdf-test.pdf");
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Note: This answer was actually proposed by user3777879. Tested, and working, he should get the credit - Stuart
Try this
I have tried to many answers but did not get good answer.
Finally got the solution with adding few code in when loading the pdf into the webview .
final WebView wv_webview= (WebView) view.findViewById(R.id.wv_webview);;
wv_webview.getSettings().setJavaScriptEnabled(true);
wv_webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wv_webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()");
}
});
String your_pdf_link="https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf";
wv_webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=" + your_pdf_link);
Note:- It will show only few milliseconds when pdf loads into webview
Output:-
webview.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('drive-viewer-toolstrip')[0].style.visibility='hidden'; })()");
I'd self-host your terms document, and I'd host it as .html file format rather than .pdf.
If you do not have a domain in which to self-host the file, check other file hosting services and see if they offer a public option that won't request login. Potential services that may work for you include Mediafire, Cramitin, Hotfile, Rapidshare, etc. (this is not an ordered or comprehensive list, do your own search).
I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});
I've got a Twitter timeline in a WebView that displays perfectly well. I followed this post as a guide. Currently I don't want to entertain other options such as Twitter4J, although I appreciate that it is a good alternative.
My problem is that I can't click any links, pictures, or otherwise interact with the timeline other than scrolling up and down. I get the following error. Any way around this?
05-29 19:09:10.887: I/chromium(13226): [INFO:CONSOLE(0)] "Refused to display 'https://mobile.twitter.com/XXXXXXXX/status/YYYYYYYYYY/photo/1' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.", source: https://twitter.com/XXXXXXXX/status/YYYYYYYYYY/photo/1 (0)
Here's my code:
// Locate the WebView in fragment_twitter.xml
WebView tweetWebView = (WebView) V.findViewById(R.id.tweetWebView);
// Settings for the WebView
tweetWebView.setBackgroundColor(0);
tweetWebView.getSettings().setJavaScriptEnabled(true);
tweetWebView.getSettings().setDomStorageEnabled(true);
// Load the WebView with the imageURL
String timelineWidget = "<a class=\"twitter-timeline\" data-dnt=\"true\" href=\"https://twitter.com/XXXXXXXX\" data-widget-id=\"SECRETNUMBER\">Tweets by #XXXXXXXX</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.async=true;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
tweetWebView.loadDataWithBaseURL("https://twitter.com", timelineWidget, "text/html", "UTF-8", null);
My problem was same as above but the requirement was different, it was to open the link in the embedded webview and then open inside webview link in the native browser,
mWebView.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Logger.logD(TAG, "URL = " + url);
if(mWebView.getVisibility() == View.VISIBLE && !HomeActivity.isPaused()) {
//Open url in native browser
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return null;
}
return super.shouldInterceptRequest(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
//show embedded webview and hide the progress bar
mWebView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
In Add boolean in Your activity
#Override
protected void onResume() {
super.onResume();
isPaused = false;
}
#Override
protected void onPause() {
super.onPause();
isPaused = true;
}
I hope few changes could lead you to the solution, Thanks