How to handle facebook like with confirm in android webview
Asked Answered
S

2

3

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.

Saba answered 20/9, 2011 at 13:22 Comment(2)
I have solved this issue by over ridding onCreateWindow() method of WebChromeClient and enabling these two options for webview. likeWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); likeWebView.getSettings().setSupportMultipleWindows(true); Thanks to all.Saba
you should have post this as answer and accept your own answer so other can get to know,easily.Medardas
S
15

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.

Saba answered 5/11, 2011 at 18:27 Comment(2)
Can you please tell if your code is solution for my problem as well facebook.#16830100Gargantua
Hi, I did this exact same thing (more or less). But it crash between Android 4.0 and Android 4.3 (only works in Android 4.4 and above). The crash is native [Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 1637 (WebViewCoreThre)]. And before that I see this warning Invalid indirect reference 0xa74a9fd0 in decodeIndirectRef. Is this working for you on Android 4.1 / 4.2 / 4.3?Savour
C
9

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.

Confirmation answered 28/1, 2014 at 11:20 Comment(3)
that was pretty useful but i have one question can you tell me what is the content of ApkfWebViewClient and where you defined itMononuclear
good catch! ApkfWebViewClient is a child class of WebViewClient (with custom shouldOverrideUrlLoading, onPageStarted, onPageFinished, onReceivedError...) So just use WebViewClientConfirmation
to be honest after i asked i rechecked the code and i noticed why you used it but thanks for exlaination by the way it might help someone elseMononuclear

© 2022 - 2024 — McMap. All rights reserved.