Gobi Krishna
Gobi Krishna

Reputation: 59

Facebook like and comment not working on Android webview

I have tried webview with Android studio and FB share and other social media share buttons are working fine. But FB direct like and comment button on my website is not working. When I press like button I can see white screen with left top screen number "1". Please kindly help me.

MainActivity.Java

package com.example.neermaicom;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static android.content.Intent.*;

public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

    try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    } catch (Exception e) {

        return true;
    }
}
});
webView.loadUrl("http://www.neermai.com");

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}

//This method require to use back button if want to go previous web page

 @Override
 public void onBackPressed() {

 if(webView.canGoBack()){
    webView.goBack();
 }else {
    super.onBackPressed();
 }
 }

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

Upvotes: 1

Views: 866

Answers (1)

Eduardo Freitas
Eduardo Freitas

Reputation: 23

This is due to the Facebook user auth, unfortunately android webview is not fully compatible with auth systems, an alternative that I use in my webview applications is to use "Chrome Custom Tabs" to auth access to the system of comments that I use.

In my application when the user clicks on comment, a Google Chrome tab is opened inside my application showing the comments page, when the user is not logged, the chrome auth the user without having to close or minimize my application to open google chrome. Everything is done within my own application.

To start the Chrome Tab when I added an "Intent" to shouldOverrideUrlLoading, below is an example of my code.

 @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if  (url.contains("#comments")){
        Uri uri = Uri.parse(url);
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.build().launchUrl(PostActivity.this, uri);
        return true;
        }
        else {
        view.loadUrl(url);
        return true;
        }
    }

And in Gradle app:

implementation 'androidx.browser:browser:1.3.0-alpha01'

My url for comment pages has "#comments" you change to something standard that you have in your comment page URLs or if the facebook comment system is directly on the post page, put something that is standard in the url of your posts to open in the google chrome tab.

Upvotes: 1

Related Questions