Search code examples
androidfacebook-likefacebook-android-sdk

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 the code snippet i am using.

    private void setUpWebView() {
    likeWebView.setVerticalScrollBarEnabled(false);
    likeWebView.setHorizontalScrollBarEnabled(false);
    likeWebView.setWebViewClient(new FacebookWebViewClient());
    likeWebView.setWebChromeClient(new MyChromeClient());
    likeWebView.getSettings().setJavaScriptEnabled(true);
     String url = getFacebookLikeUrl();
    likeWebView.loadUrl(url);
 }

I am also setting ViewClient and WebChromeClient.

private String getFacebookLikeUrl() {
    return "http://www.facebook.com/plugins/like.php?" + "href="
            + URLEncoder.encode(mUrl) + "&access_token="
            + URLEncoder.encode(facebook.getAccessToken());
}

Please help me out to solve this issue. Thanks in advance.


Solution

  • 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.