I have a webview in my application which upon launch displays an html page. The page has a hyperlink which on click is supposed to display a video.
When i run the application and click on the video hyperlink link , nothing happens. But if i load the same page in android browser, then it launches a default video player and everything works fine.
I debugged it furthers by putting a log statement in shouldOverrideUrlLoading method and noticed that, when the hyperlink is clicked it gets redirected to another link and then to another link (final video streaming url).
My question is : why would the link work perfectly in default android browser and not through a webview.
Thanks
What is happening is when you click the hyperlink, that link probably has some popups inside of it. You need to define the onCreateWindow function in your webview's WebChromeClient. This handles how calls to open new windows or popups are handled.
public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
((WebView.WebViewTransport) resultMsg.obj).setWebView(myWebView);
resultMsg.sendToTarget();
return true;
}
After declaring your WebView you should set javascript enabled, then your WebView will work as a browser.
For example:
WebView mwebview = new WebView(this);
setContentView(mwebview);
mwebview.getSettings().setJavaScriptEnabled(true);
or
mwebview.getSettings().setPluginState(PluginState.ON); // this is for newer API's
Basically, do not expect your embedded WebView works the same as Android default Browser. The default Browser is built on the same WebView, but there are lots a customization. (Especially around the no-standard uri, HTML5 stuff)
I followed code from here: WebView and HTML5 <video>, and I put the video link to a video tag, and I got the Video playing in my own version of WebView. The behavior is a little different from the default Browser. Given more time, we could figure that out by looking at its code, but anyways ...
Related
I am trying to write an app I want it to open a web page and auto login I am not sure how to go about sending the info to the browser from the app code.
So basically you are going to need to load in the webpage within a WebView (You can find instructions for that here and then probably push javascript into the WebView that will fill in the fields and load the page.
In your activity's onCreate:
WebView webview = new WebView(this);
setContentView(webview);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean onPageFinished(WebView view, String url) {
// Check here if url is equal to your site URL.
}
});
webview.loadUrl("http://yourwebsite.com/");
This line enables javascript in your WebView:
webView.getSettings().setJavaScriptEnabled(true);
Then you can use the WebViewClient to detect when the page you want has fully loaded. When that happens, you can use:
webView.loadUrl("javascript:document.getElementsByName('username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('password').value = 'password'");
webView.loadUrl("javascript:document.forms['login'].submit()");
And it should automatically log you in. It's worth noting that this generally isn't easy to do on a lot of sites since they will randomize the login control ids and it also doesn't generally sit well with users if an application is logging into a website automatically for them.
Good day!
Guys, I have a problem and want your help.
Through a WebView, how do I get when clicked by the user, do not open the options of standard browsers (eg chrome and internet). I saw an app by clicking the link to the page opens normally however, as if open in a custom browser. I found it very interesting, and I wonder how this mechanics.
If anyone know how to create an app that perform the same procedure I am grateful.
For better doubt the APP is this:
https://play.google.com/store/apps/details?id=com.tachanfil.jornaisdobrasil&hl=pt-BR
You have to set up WebViewClient like
this.mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Then your app will display all links in your WebView instead of opening browser
Perhaps you are missing setting your client as the webview client - Refer to
http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient%28android.webkit.WebViewClient%29
I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.
I'm hoping someone can set the record straight for me. I know there are several posts about this issue, but I'm still unclear about this.
I want my application to show an image of a page out of a book, and inside that image I want to play a youtube video. Apparently, I"m supposed to use a WebView for this.
WebView mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
setContentView(mWebView);
String Play = "<iframe width=\"600\" height=\"400\" src=\"http://www.youtube.com /embed/VCDOjdYhj1Y\" frameborder=\"10\"</iframe>";
mWebView.loadData(Play, "text/html", "utf-8");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
When I do this, the video looks okay but the sound is lagging. It's very noticeable if someone is talking because the words don't match their lips. From what I understand, WebView really isn't meant for showing video even though it's supported. It's simply a way of showing a web page inside of the app with preferably no user interaction. If you want video and web pages, you're supposed to launch the browser with an Intent. I don't want to do this. I can't use a VideoView because I'm not pointing directly to a video file.
So is that it? Am I out of options? You'd think Youtube and Android would be very compatible since their both Google. Embedding a youtube video in an HTML View works really well in iPhone apps. I hope someone can give me the final word on this. Thanks!
Can't it is something with how the url is loaded like parents, etc.
I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}