possible WebView OnCreateWindow to make popup window(Dialog style)? like android browser
When I press a button with this code on ebay.com in my webview:
html of button:
input id="addPicturesBtn" name="addPictu开发者_开发百科resBtn" type="button" value="Add pictures" class="button" onclick="ebay.run(this.id,'onclick');return false;" onkeydown="ebay.run(this.id,'onkeydown');return false;" title="Add pictures"
If this button is pressed inside my webview, It sends the new window from my webview to the android browser. But if the same button is pressed from within the android browser it pops up this cool dialog type popup window (see picture)
I would like to open the popup style window on eBay like the browser does, if this button is pressed inside my webview. So that it can be closed by the user to return them to my app behind it when they are done with the popup.
Is that possible?
Here is what I have so far:
webChromeClient = new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
WebView childView = new WebView(Ebay.this);
final WebSettings settings = childView.getSettings();
settings.setJavaScriptEnabled(true);
childView.setWebChromeClient(this);
childView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
Am I missing something?
Here is a picture of the android browser popup (That I'm trying to get my webview to launch from the button on ebay.com):
Use javascript interface. Combine javascript with java and make custom dialog in java At first, enable javascript
webview.getSettings().setJavaScriptEnabled(true);
On second, you need to add javascript interface
wv.addJavascriptInterface(new DemoJavaScriptInterface(), "js");
And third, load url;
webview.loadUrl("javascript:[javascript function name here]()");
Here is some code for you, hope this can help https://github.com/scottagarman/Android-JavaScript-Interface-Example
Code credit go to the developer called "scottagarman" .
You can do it his way:
public void onCreate(Bundle savedInstanceState){
....
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJSInterface(), "myappname");
webView.loadUrl("http://mysite.com/"); // or some local asset
....
}
public class MyJSInterface{
....
public void popup(){
// AlterDialog etc.
}
....
}
HTML from your website:
window.onload = function(){
window.myappname.popup();
}
I wish it would be useful for you...
By the way, be careful with 2.3 version it had a corresponding bug, just FYI))
精彩评论