I am trying to implement facebook like functionality using android webview. It is working fine without \"confirm\" dialog. But its not working when like needs confirmation.
Facebook like confirmation opens confirm_widget in new window. So your webview should support Multiple Window opening. for this setJavaScriptCanOpenWindowsAutomatically(true) and setSupportMultipleWindows(true) for your webview-
private void setUpWebView() {
likeWebView = new WebView(getContext());
likeWebView.setWebViewClient(new FacebookWebViewClient());
likeWebView.setWebChromeClient(new MyChromeClient());
final WebSettings webSettings = likeWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
String url = getFacebookLikeUrl();
likeWebView.loadUrl(url);
likeWebView.setLayoutParams(FILL);
mContent.addView(likeWebView);
}
Facebook like confirmation calls onCreateWindow() method. SO override the onCreateWindow method in WebChromeClient -
final class MyChromeClient extends WebChromeClient {
// Add new webview in same window
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
WebView childView = new WebView(getContext());
childView.getSettings().setJavaScriptEnabled(true);
childView.setWebChromeClient(this);
childView.setWebViewClient(new FacebookWebViewClient());
childView.setLayoutParams(FILL);
mContent.addView(childView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
return true;
}
// remove new added webview whenever onCloseWindow gets called for new webview.
@Override
public void onCloseWindow(WebView window) {
mContent.removeViewAt(mContent.getChildCount() - 1);
}
}
confirm_widget for like calls onCloseWindow when user click either Like or Cancel. On this method remove last added webview.
I used this for Stripe Checkout which opens a new window in mobile devices for payments.
Based on @Shweta's response :
In your activity:
package myapp.app;
/*** imports ***/
public class LoggedActivity extends FragmentActivity
{
public WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.logged);
// retrieve the main container
LinearLayout container = (LinearLayout) findViewById(R.id.logged_webviews_container);
// layout params applied to the webviews in order to fit 100% the parent container
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
myWebView = new WebView(this);
myWebView.setLayoutParams(layoutParams);
myWebView.setWebViewClient(new BetterWebViewClient(this));
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setSupportMultipleWindows(true);
// on this instruction, we set our extended class below as the Parent Webview webchromeclient
myWebView.setWebChromeClient(new PopupWebView(this, myWebView, container, layoutParams));
// load URL
myWebView.loadUrl('http://www.mywebsite.com');
container.addView(myWebView);
}
}
Add this class which extends WebChromeClient
package myapp.app;
/*** imports ***/
public class PopupWebChromeClient extends WebChromeClient {
protected Activity activity;
protected WebView parentWebView;
protected RelativeLayout container;
protected WebView popupView;
PopupWebChromeClient(
Activity activity,
WebView parentWebView,
RelativeLayout container
)
{
super();
this.activity = activity;
this.parentWebView = parentWebView;
this.container = container;
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
this.parentWebView.setVisibility(WebView.GONE);
this.popupView = new WebView(this.activity);
// setup popuview and add
this.popupView.getSettings().setJavaScriptEnabled(true);
this.popupView.setWebChromeClient(this);
this.popupView.setWebViewClient(new ApkfWebViewClient(this.activity, true));
this.popupView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT
));
this.container.addView(this.popupView);
// send popup window infos back to main (cross-document messaging)
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(popupView);
resultMsg.sendToTarget();
return true;
}
// remove new added webview on close
@Override
public void onCloseWindow(WebView window) {
this.popupView.setVisibility(WebView.GONE);
this.parentWebView.setVisibility(WebView.VISIBLE);
}
}
In your layout xml, don't set webviews since we create them on the fly.