Android unable to implement facebook comment in a webview due to default browser

前端 未结 1 328
心在旅途
心在旅途 2020-12-14 05:01

I am newbie to Android development and this webview and webview client is killing me. This is my scenario:

  1. I have to load a web page which contains facebook so
相关标签:
1条回答
  • 2020-12-14 05:24

    The answer provided on How to handle facebook like with confirm in android webview is the best solution I have found so far.What I learnt is that Android WebView doesn't by default load the popup of html views and for that we need to use WebChromeClient which handles all these.Look at the following code

     private String requestUrl="some web link containing facebook social comment";
     private WebView webView,childView =null;
     private LinearLayout parentLayout;
     private Activity MyActivity;
     @Override
     public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    
        setContentView(R.layout.main);
    
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);
        parentLayout =(LinearLayout)findViewById(R.id.parentLayout);
    
    
        MyActivity = this;
    
    
        webView = new WebView(this);
        webView.setLayoutParams(getLayoutParams());
    
        webView.setWebViewClient(new FaceBookClient());
        webView.setWebChromeClient(new MyChromeClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setSupportMultipleWindows(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
    
        parentLayout.addView(webView);
        webView.loadUrl(requestUrl);
    
     }
    
      private LinearLayout.LayoutParams getLayoutParams(){
        return new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
    }
    
    
        final class MyChromeClient extends WebChromeClient{
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog,
                boolean userGesture, Message resultMsg) {
            childView = new WebView(SmCommentTestActivity.this);
            childView.getSettings().setJavaScriptEnabled(true);
            childView.getSettings().setSupportZoom(true);
            childView.getSettings().setBuiltInZoomControls(true);
            childView.setWebViewClient(new FaceBookClient());
            childView.setWebChromeClient(this);
            childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    
    
            parentLayout.addView(childView);
    
    
            childView.requestFocus();
            webView.setVisibility(View.GONE);
    
              /*I think this is the main part which handles all the log in session*/
            WebView.WebViewTransport transport =(WebView.WebViewTransport)resultMsg.obj;
            transport.setWebView(childView);
            resultMsg.sendToTarget();
            return true;
        }
    
    
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            MyActivity.setProgress(newProgress*100);
        }
    
        @Override
        public void onCloseWindow(WebView window) {
            parentLayout.removeViewAt(parentLayout.getChildCount()-1);
            childView =null;
            webView.setVisibility(View.VISIBLE);
            webView.requestFocus();
        }
    }
    
        private class FaceBookClient extends WebViewClient{
         @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i("REQUEST URL",url);
            return false;
        }   
    }
    
        @Override
        public void onBackPressed() {
        if(childView != null && parentLayout.getChildCount()==2){
            childView.stopLoading();
            parentLayout.removeViewAt(parentLayout.getChildCount()-1);
            if(webView.getVisibility() == View.GONE)
                webView.setVisibility(View.VISIBLE);
        }else{          
            super.onBackPressed();
        }
    }
    

    This is all one has to do to implement Facebook Social Comment Plugin on Android WebView and if somebody finds any flaw in it,then I would be happy to correct it.And hope,this solution would save and time on any troubled developer like me ;)

    0 讨论(0)
提交回复
热议问题