Android. Fit webview size to youtube video size - android

I'd like to play youtube video by using android webview. But I want to fit my webview to youtube video size. Until now, my implementaion is like below.
String vid = TextUtils.extractYoutubeVId(body);
if (StringUtils.isBlank(vid)) {
return null;
}
String html = "<html > <head></head> <body style=\"margin:0 0 0 0; padding:0 0 0 0;\"> <iframe width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/___\" frameborder=\"0\"></iframe> </body> </html> ";
html = html.replace("___", vid);
mImportedVideoWebview.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = mImportedVideoWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mImportedVideoWebview.loadData(html, "text/html", "utf-8");
mImportedVideoWebview is just a WebView and its width is MATCH_PARENT and height is 350dp.
By using above code, I can play youtube video.
But, I want to put my webview width to screen width and fit my webview's height to youtube video height proportionally. Anyone help me plz!
Thanks!

Use below code snipe hope it will
<html><body><iframe frameborder=0 allowfullscreen width=100% height=100% src=link frameborder=0 allowfullscreen></iframe></body></html>

You can use:
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);

Related

Showing iframes in android webview correctly

I want to show some html content in webView. Here is my code:
String htmlContent = "..."; // some content here
webViewContent.getSettings().setJavaScriptEnabled(true);
webViewContent.setWebViewClient(new WebViewClient());
webViewContent.setHorizontalScrollBarEnabled(false);
webViewContent.setHorizontalScrollBarEnabled(false);
webViewContent.getSettings().setSupportMultipleWindows(false);
webViewContent.getSettings().setAllowFileAccess(true);
webViewContent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webViewContent.setWebChromeClient(new WebChromeClient());
webViewContent.setBackgroundColor(ContextCompat.getColor(context, R.color.background_final_page));
webViewContent.loadDataWithBaseURL(null, htmlContent , "text/html", "UTF-8", null);
It shows text and images well, but when i have iframe in htmlContent , for example
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="https://www.facebook.com/plugins/post.php?href=link" width="100%"></iframe>
webView shows iframe not correctly, with cropped height.When i added height="100%" to iframe code, it has no result. When i set height=600 or height=400, it works, but i cannt know in advance height size of iframe.
So, how i can to fix it?

Vimeo iframe in webview

how is it possible to view a video from vimeo embed code in a webview in android?
I only hear the audio but the video stays black.
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient());
wb.setWebChromeClient(new WebChromeClient());
wb.loadDataWithBaseURL(wb.getUrl(),
"<iframe src=\"http://player.vimeo.com/video/**********?title=0&byline=0&portrait=0\" width=\"500\" height=\"281\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>",
"text/html",
"utf-8",
null);
way too late to the party here,
i didnt use i did it like this and it works for me.
webView.loadUrl("http://player.vimeo.com/video/" +"insert video id here"+ "?player_id=player&autoplay=1&title=0&byline=0&portrait=0&api=1&maxheight=680&maxwidth=1000");
and change width and height according to your needs

Android webview cannot render youtube video embedded via iframe

This is about loading youtube videos using latest embedded format (iframe) inside a webview.
Example of the iframe embed format
<iframe width="637" height="358" src="http://www.youtube.com/embed/olC42gO-Ln4?fs=1&feature=oembed" frameborder="0" allowfullscreen=""></iframe>
Test the code on Android 2.3.3 & 3.2 devices (HTC Desire & Asus Transformer), the webview would only show a black rectangle.
I tried a similar embed from vimeo
<iframe src="http://player.vimeo.com/video/35693267" width="640" height="360" frameborder="0"></iframe>
In 2.3, video played correctly
In 3.2, a black rectangle flashed and disappeared, the iframe area is blank.
Finally if the old embed format (using the object tag) is used, the video is displayed properly inside the webview in both 2.3.3 & 3.2.
I have checked related questions and added
android:hardwareAccelerated="true"
in the application and/or activity tag but still no video in both 2.3 & 3.2 devices.
This is a big problem because more websites are now using the newest format (iframe) to embed their youtube videos. Android/Youtube Team, please take a look at this problem.
Android browsers are utterly buggy what comes to video playback and embedding. It simply does not work across devices. Trying to get it working is just waste of your time. My suggestion is that you don't try to include <iframe> but simply provide a thumbnail of the video which directly links to YouTube page or h264 file.
Earlier discussion, with a possible solution.
Google Reader-esque optimizing of WebViews on Android
If you want to play videos within your WebView you NEED to load the data with a base URL!
DONT do this:
mContentWebView.loadDataWithBaseURL(null, webViewContentString,
"text/html", "UTF-8", null);
DO THIS INSTEAD:
//veryVeryVery important for playing the videos!
mContentWebView.loadDataWithBaseURL(theBaseUrl, webViewConentString,
"text/html", "UTF-8", null);
The Base URL will be the something like the "original" url of what you are displaying in your WebView. So let's say you are making a news reader, the WebView's base url will be the url of the original story.
Good Luck!
Also remember to set up your WebView...Like so...
mContentWebView.setWebChromeClient(new WebChromeClient());
mContentWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mContentWebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
mContentWebView.setWebViewClient(new WebViewClient());
mContentWebView.getSettings().setJavaScriptEnabled(true);
You need to have hardware acceleration turned on in the Manifest (only available on SDK 14 and above).
Ex. Hardware Acceleration On:
<application
android:name="com.example.app"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:hardwareAccelerated="true">
<!-- hardwareAccelerated requires SDK 14 -->
...
</application>
HTML5 Video support
In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.
http://developer.android.com/reference/android/webkit/WebView.html
(Hope it help someone)
This worked for me- the code opens youtube site and can play its videos inside WebView:
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"320\" height=\"315\" src=\"https://www.youtube.com/\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
mWebView.loadData(frameVideo, "text/html", "utf-8");
mWebView.loadUrl("http://www.youtube.com/");
mWebView.setWebViewClient(new WebViewClient());
I would suggest using some code to detect the environment of the user... use the iframe code only for ios devices (iphone, ipod, ipad) and use the old code for everyone else.
This Code made exactly fit to different device
webView.setInitialScale(1);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Log.e(SimpleBillsConstants.SIMPLE_BILLS, width + "-" + height);
String data_html = "<!DOCTYPE html><html> <head> <meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"target-densitydpi=high-dpi\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" media=\"screen and (-webkit-device-pixel-ratio:1.5)\" href=\"hdpi.css\" /></head> <body style=\"background:black;margin:0 0 0 0; padding:0 0 0 0;\"> <iframe style=\"background:black;\" width=' "+width+"' height='"+height+"' src=\""+ VIDEO_URL+"\" frameborder=\"0\"></iframe> </body> </html> ";
webView.loadDataWithBaseURL("http://vimeo.com", data_html, "text/html", "UTF-8", null);

Youtube embed videos not working in webview. What's wrong with this code?

I am new to android. I am facing two problems right now 1. I parsed xml file and showed it in webview which contains videos and text . When i click on videos it will not play . In xml videos are youtube embed.
Why is it so?
1.First problem :code and image
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Intent intent=getIntent();
String urlsting=intent.getStringExtra("str");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setInitialScale(100);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadDataWithBaseURL(null, urlsting,"text/html", "utf-8", null);
}
When i clcik on video it starts browsing and shows only black screen,then nothing will happen.
![][1]
2. I have you-tube embedded videos url which I given in html tag and try to load it .They are also not working for me. I checked number of questions and blogs and also made no of settings then also unable to show video . Help will be appreciated .Thanks in advance.
String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/lBMMTeuJ_UQ?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/BZMkY3y7nM0?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td></table>";
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setPluginsEnabled(true);
//mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null);
mWebView.loadData(video,"text/html","UTF-8");
In this case I load the youtube embeded videos to webview but they are also not working.
![][2]
I am using android 2.3.3version.I also want clarification that is there any requirement of install adobe flash player on emulator, but i think no because videos are working in browser. can anybody tell i am right or wrong?? please try to give me solution because i stuck with this problem long ago..
I also tried using object tag as follows:
String obj="<object width=\"300\" height=\"260\"><param name=\"movie\" value=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"?version=3&hl=pt_BR&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\" ?version=3&hl=pt_BR&rel=0\" type=\"application/x-shockwave-flash\" width=\"480\" height=\"330\" allowscriptaccess=\"always\" allowfullscreen=\"true\" /></object>";
You need to
set a WebChromeClient to your WebView
web.setWebChromeClient(new WebChromeClient());
and turn on hardwareAccelerated value
<application
android:hardwareAccelerated="true"...
check out http://developer.android.com/reference/android/webkit/WebView.html and read HTML5 Video support part.
This works for me...
I guess, you are using iframe tag in your html code. Change it with object tag. I mean, you must use object instead iframe. An example here:
<object width="480" height="330"><param name="movie"
value="http://www.youtube.com/v/"here is your video id"?version=3&hl=pt_BR&rel=0">
</param><param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/"here is your video id"?version=3&hl=pt_BR&rel=0"
type="application/x-shockwave-flash" width="480" height="330" allowscriptaccess="always" allowfullscreen="true" /></object>
Use the code below:
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setAllowFileAccess(true);
webview.loadUrl(url);
Try this and thick it as accepted if your problem solved :)
have you tested your app in actual device? Because the videos which are embedded like this i.e. iFrame are taking more time to get loaded. In my case, same thing was happening then I changed the webview settings to,
getSettings().setPluginsEnabled(true);
but after that it was not working on emulator and in device it was loading so I thought its not working but just today I tried the same thing and it was working fine with my surprise. So you just need to add this line of code.
Second thing is that only those videos which are embedded in iframe tag are supported by enabling plugins to true.
This does it:
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.setWebChromeClient(new WebChromeClient());
And enable hardware acceleration too!
Follow this source, Youtube video should play
String youtubeId = "iiLepwjBhZE";
int width = rl.getWidth();
int height = rl.getHeight();
WebChromeClient mWebChromeClient = new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
/*
view.loadUrl("javascript:(function() { " +
"document.querySelector('iframe').setAttribute('style', 'width: 1080px; height: 1920px'); " +
"})()");
*/
}
};
String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"" +
width +
"\" height=\"" +
height +
"\"frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/" +
youtubeId +
"?enablejsapi=1&origin=example.com\"></iframe> </td> </tr><tr><td></table>";
//
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setInitialScale(1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
//mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null);
webView.loadData(video,"text/html","UTF-8");

Fit image in webview

I am planning to display images from SD card in a webview in order to take advantage of he built in zoom capabilities of webview. However, I am facing an issue with displaying images that are bigger than screen size (e.g. 1800x1200) to fit the screen initially, like in an ImageView. I want the image to be displayed in full at first and provide zoom control to the users.
I have tried using WRAP_CONTENT for webview's width and height, but that does not work.
Any ideas?
Following is a code snippet I am using:
String path = getRealPathFromURI(mUriList.get(0)); // this gets the file path
webView = (WebView) findViewById(R.id.WebView01);
WebSettings settings= webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
webView.loadUrl("file://" + path);
private WebView mWebView2;
mWebView2 = (WebView)findViewById(R.id.webview);
mWebView2.getSettings().setJavaScriptEnabled(true);
mWebView2.getSettings().setLoadWithOverviewMode(true);
mWebView2.getSettings().setUseWideViewPort(true);
mWebView2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView2.setScrollbarFadingEnabled(true);
mWebView2.loadDataWithBaseURL("file:///android_asset/", "<img src=\"banner5.png\" height=\"98%\" width=\"100%\"/>", "text/html", "utf-8", null);
Images are in the assets folder
If you write your HTML correctly, you don't have to do any "setLoadWithOverviewMode", "setUseWideViewPort" or "setInitialScale". And there is absolutely no reason to enable JavaScript.
This one line worked for me:
webView.loadDataWithBaseURL("file://" + directory,
"<img src=\"" + name + "\" width=\"100%\"/>", "text/html", "utf-8", null);
The underlining HTML code is:
<img src=YourImage.png width="100%" />, by not setting a height, its aspect ratio will be kept.
That did the trick for me:
webView.setInitialScale(30);
WebSettings webSettings = webView.getSettings();
webSettings.setUseWideViewPort(true);
WebSettings settings = webView.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true)

Categories

Resources