Android Studio WebView Youtube Video BlackScreen - java

I want to play a video in the WebView but somehow I get an blackscreen on the video but I can hear the sound of it. Would be really glad if someone can help me
public class MainActivity extends AppCompatActivity {
WebView displayYoutubeVideo;
TextView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String frameVideo = "<html><body>Video From YouTube<br><iframe
width=\"560\" height=\"315\"
src=\"https://www.youtube.com/embed/WtO3_tF8niU\" frameborder=\"0\"
allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.youtube_view);
displayYoutubeVideo.getSettings().setUserAgentString("Mozilla/5.0
(Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
}
}

I guess you might be missing android:hardwareAccelerated="true" in the permissions of your activity in the manifest file. Since you are playing a flash video this should work out to make the WebView flash enabled. This is something that I encountered and the resolution was to add this permission.

This link may help you.It seems you missed some properties for the web view.
https://inducesmile.com/android/embed-and-play-youtube-video-in-android-webview/

You must activate Hardware acceleration if your android:targetSdkVersion is lower than 11 and have to set webChromeclient to your webview
displayYoutubeVideo.setWebChromeClient(new WebChromeClient());
and in your manifest file add below line
<application android:hardwareAccelerated="true" ...>

Related

How to open new intent when webview go out of my website

I'm making an Android App with Java in Android Studio for my website using webview, but i've a lot of links which go out of website. How can i detect if link redirect from www.mywebsite.com to www.anotherwebsite.com and open it in another page intent?
You can use the code below.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
and create and inner class
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
You can find complete documentation here

How to setup Android WebView to make it work identically Android browser?

What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview

Mobile site in webview doesn't work

Well i'm working for a simple webview to display a website of course..this website has a mobile version but when i try to run the application the webview displays the desktop mode. I tryied every way but nothing seems to work.. I tryied to change the user agent but nothing..
This is the part of user agent i'm using now
webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U;`
Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like
Gecko) Version/4.0 Mobile Safari/530.17");
Of course doesn't work.
Another one (doesn't work)
webview.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.2.2; Nexus 7Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19");
And many others but none works. What can i do?
EDIT with my code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Request progress circle
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
setProgressBarIndeterminateVisibility(true); // Show progress circle
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
setProgressBarIndeterminateVisibility(false); // Hide progress circle when page loaded
activity.setTitle("Title");
}
});
if (savedInstanceState == null)
{
webview.loadUrl("http://forum.ubuntu-it.org/");
}
}
Use the Following Snippet hope this will work with you
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
[...] when i try to run the application the webview displays the desktop
mode.. [...] I tryied to change the user agent but nothing..
Try to remove "webview.getSettings().setUserAgentString(.. "..
Android open in "mobile mode" by default..
[...] when i try navigate and click in some link it ask if i want open
it with a browser.
To open links clicked by the user inside your WebView, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
http://developer.android.com/guide/webapps/webview.html

Webview start always in desktop mode

It's two days i can't start my webview directly in mobile mode. When i open the application for the first time starts the webview displays the desktop. My code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Request progress circle
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
setProgressBarIndeterminateVisibility(true); // Show progress circle
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
setProgressBarIndeterminateVisibility(false); // Hide progress circle when page loaded
activity.setTitle("Title");
}
});
if (savedInstanceState == null)
{
webview.loadUrl("http://forum.ubuntu-it.org/");
}
}
Even if i changed the User Agent doesn't work! I tryied every way. I hope in a help from someone, Thanks.
Add this line before loadUrl()
webview.setWebViewClient(new WebViewClient());

WebView only opens some URLs but not all

When I make a call to my WebView and give it a URL I want it to open within the app (i.e. on my page) but it doesn't always do this.
Some pages will open within my app but others will open in the default android web browser.
Here's my code. Any help on this would be much appreciated.
WebView webView = ((WebView)findViewById(R.id.website));
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(website);
To override the default behavior, use something like:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
More or less duplicate of the existing question Clicking URLs opens default browser?

Categories