How To Handle Facebook Like With Confirm In Android Webview
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. Here is t
Solution 1:
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-
privatevoidsetUpWebView() {
likeWebView = newWebView(getContext());
likeWebView.setWebViewClient(newFacebookWebViewClient());
likeWebView.setWebChromeClient(newMyChromeClient());
finalWebSettingswebSettings= likeWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
Stringurl= getFacebookLikeUrl();
likeWebView.loadUrl(url);
likeWebView.setLayoutParams(FILL);
mContent.addView(likeWebView);
}
Facebook like confirmation calls onCreateWindow() method. SO override the onCreateWindow method in WebChromeClient -
finalclassMyChromeClientextendsWebChromeClient {
// Add new webview in same window@OverridepublicbooleanonCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
WebViewchildView=newWebView(getContext());
childView.getSettings().setJavaScriptEnabled(true);
childView.setWebChromeClient(this);
childView.setWebViewClient(newFacebookWebViewClient());
childView.setLayoutParams(FILL);
mContent.addView(childView);
WebView.WebViewTransporttransport= (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
returntrue;
}
// remove new added webview whenever onCloseWindow gets called for new webview.@OverridepublicvoidonCloseWindow(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.
Solution 2:
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 ***/publicclassLoggedActivityextendsFragmentActivity
{
public WebView myWebView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.logged);
// retrieve the main containerLinearLayoutcontainer= (LinearLayout) findViewById(R.id.logged_webviews_container);
// layout params applied to the webviews in order to fit 100% the parent container
LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
myWebView = newWebView(this);
myWebView.setLayoutParams(layoutParams);
myWebView.setWebViewClient(newBetterWebViewClient(this));
WebSettingssettings= 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(newPopupWebView(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 ***/publicclassPopupWebChromeClientextendsWebChromeClient{
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;
}
@Overridepublic boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
this.parentWebView.setVisibility(WebView.GONE);
this.popupView = new WebView(this.activity);
// setup popuview and addthis.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();
returntrue;
}
// remove new added webview on close@Overridepublic 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.
Post a Comment for "How To Handle Facebook Like With Confirm In Android Webview"