Open Facebook page from Android app?

AndroidFacebookUrl Scheme

Android Problem Overview


from my Android app, I would like to open a link to a Facebook profile in the official Facebook app (if the app is installed, of course). For iPhone, there exists the fb:// URL scheme, but trying the same thing on my Android device throws an ActivityNotFoundException.

Is there a chance to open a Facebook profile in the official Facebook app from code?

Android Solutions


Solution 1 - Android

This works on the latest version:

  1. Go to https://graph.facebook.com/<user_name_here> (https://graph.facebook.com/fsintents for instance)

  2. Copy your id

  3. Use this method:

     public static Intent getOpenFacebookIntent(Context context) {
    
        try {
        	context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
     	return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
        } catch (Exception e) {
     	return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
        }
     }
    

This will open the Facebook app if the user has it installed. Otherwise, it will open Facebook in the browser.

EDIT: since version 11.0.0.11.23 (3002850) Facebook App do not support this way anymore, there's another way, check the response below from Jared Rummler.

Solution 2 - Android

In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ no longer work. I decompiled the Facebook app and found that you can use fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]. Here is the method I have been using in production:

/**
 * <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
 * default web browser will be used.</p>
 *
 * <p>Example usage:</p>
 *
 * {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
 *
 * @param pm
 *     The {@link PackageManager}. You can find this class through {@link
 *     Context#getPackageManager()}.
 * @param url
 *     The full URL to the Facebook page or profile.
 * @return An intent that will open the Facebook page/profile.
 */
public static Intent newFacebookIntent(PackageManager pm, String url) {
  Uri uri = Uri.parse(url);
  try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
    if (applicationInfo.enabled) {
      // http://stackoverflow.com/a/24547437/1048340
      uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    }
  } catch (PackageManager.NameNotFoundException ignored) {
  }
  return new Intent(Intent.ACTION_VIEW, uri);
}

Solution 3 - Android

Is this not easier? For example within an onClickListener?

try {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/426253597411506"));
   	startActivity(intent);
} catch(Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/appetizerandroid")));
}

PS. Get your id (the large number) from http://graph.facebook.com/[userName]

Solution 4 - Android

For Facebook page:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + pageId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + pageId));
}

For Facebook profile:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + profileId));
}

...because none of the answers points out the difference

Both tested with Facebook v.27.0.0.24.15 and Android 5.0.1 on Nexus 4

Solution 5 - Android

Here's the way to do it in 2016. It works great, and is very simple to use.

I discovered this after looking into how emails sent by Facebook opened the Facebook app.

// e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should
// put EXAMPLE_PAGE at the end of this URL, after the ?
String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));

startActivity(browserIntent);

Solution 6 - Android

this is the simplest code for doing this

public final void launchFacebook() {
        final String urlFb = "fb://page/"+yourpageid;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(urlFb));

        // If a Facebook app is installed, use it. Otherwise, launch
        // a browser
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
        if (list.size() == 0) {
            final String urlBrowser = "https://www.facebook.com/"+pageid;
            intent.setData(Uri.parse(urlBrowser));
        }
       
        startActivity(intent);
    }

Solution 7 - Android

A more reusable approach.

This is a functionality we generally use in most of our apps. Hence here is a reusable piece of code to achieve this.

(Similar to other answers in terms for facts. Posting it here just to simplify and make the implementation reusable)

"fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions. (Like mentioned in another answer here)

This is a full fledged working code currently live in one of my apps:

public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";

//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) { //newer versions of fb app
                return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
            } else { //older versions of fb app
                return "fb://page/" + FACEBOOK_PAGE_ID;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL; //normal web url
        }
    }

This method will return the correct url for app if installed or web url if app is not installed.

Then start an intent as follows:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

That's all you need.

Solution 8 - Android

To do this we need the "Facebook page id", you can get it :

  • from the page go to "About".
  • go to "More Info" section.

introducir la descripción de la imagen aquí

To opening facebook app on specified profile page,

you can do this:

 String facebookId = "fb://page/<Facebook Page ID>";
  startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));

Or you can validate when the facebook app is not installed, then open the facebook web page.

String facebookId = "fb://page/<Facebook Page ID>";
String urlPage = "http://www.facebook.com/mypage";

     try {
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
        } catch (Exception e) {
         Log.e(TAG, "Application not intalled.");
         //Open url web page.
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
        }

Solution 9 - Android

This has been reverse-engineered by Pierre87 on the FrAndroid forum, but I can't find anywhere official that describes it, so it's has to be treated as undocumented and liable to stop working at any moment:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
intent.putExtra("extra_user_id", "123456789l");
this.startActivity(intent);

Solution 10 - Android

try this code:

String facebookUrl = "https://www.facebook.com/<id_here>";
		try {
			int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
		    if (versionCode >= 3002850) {
		    	Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
		           startActivity(new Intent(Intent.ACTION_VIEW, uri));
		    } else {
		       	Uri uri = Uri.parse("fb://page/<id_here>");
		        startActivity(new Intent(Intent.ACTION_VIEW, uri));
		    }
		} catch (PackageManager.NameNotFoundException e) {
		    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
		}

Solution 11 - Android

As of March 2020 this works perfectly.

private void openFacebookPage(String pageId) {
    String pageUrl = "https://www.facebook.com/" + pageId;

    try {
        ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);

        if (applicationInfo.enabled) {
            int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
            String url;

            if (versionCode >= 3002850) {
                url = "fb://facewebmodal/f?href=" + pageUrl;
            } else {
                url = "fb://page/" + pageId;
            }

            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } else {
            throw new Exception("Facebook is disabled");
        }
    } catch (Exception e) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl)));
    }
}

Solution 12 - Android

After much testing I have found one of the most effective solutions:

private void openFacebookApp() {
    String facebookUrl = "www.facebook.com/XXXXXXXXXX";
    String facebookID = "XXXXXXXXX";
    
    try {
        int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
        
        if(!facebookID.isEmpty()) {
            // open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
            Uri uri = Uri.parse("fb://page/" + facebookID);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        } else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
            // open Facebook app using facebook url
            Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        } else {
            // Facebook is not installed. Open the browser
            Uri uri = Uri.parse(facebookUrl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    } catch (PackageManager.NameNotFoundException e) {
        // Facebook is not installed. Open the browser
        Uri uri = Uri.parse(facebookUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }
}

Solution 13 - Android

My answer builds on top of the widely-accepted answer from joaomgcd. If the user has Facebook installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.

Instead of:

context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));

You can use the following to decide what to do:

PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
if(info.applicationInfo.enabled)
    return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
else
    return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));

Solution 14 - Android

Best answer I have found, it's working great.

Just go to your page on Facebook in the browser, right click, and click on "View source code", then find the page_id attribute: you have to use page_id here in this line after the last back-slash:

fb://page/pageID

For example:

Intent facebookAppIntent;
try {
    facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1883727135173361"));
    startActivity(facebookAppIntent);
} catch (ActivityNotFoundException e) {
    facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/CryOut-RadioTv-1883727135173361"));
    startActivity(facebookAppIntent);
}

Solution 15 - Android

Intent intent = null;
    try {
        getPackageManager().getPackageInfo("com.facebook.katana", 0);
        String url = "https://www.facebook.com/"+idFacebook;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href="+url));
    } catch (Exception e) {
        // no Facebook app, revert to browser
        String url = "https://facebook.com/"+idFacebook;
        intent = new Intent(Intent.ACTION_VIEW);
        intent .setData(Uri.parse(url));
    }
    this.startActivity(intent);

Solution 16 - Android

As of July 2018 this works perfectly with or without the Facebook app on all the devices.

private void goToFacebook() {
    try {
        String facebookUrl = getFacebookPageURL();
        Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
        facebookIntent.setData(Uri.parse(facebookUrl));
        startActivity(facebookIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private String getFacebookPageURL() {
    String FACEBOOK_URL = "https://www.facebook.com/Yourpage-1548219792xxxxxx/";
    String facebookurl = null;

    try {
        PackageManager packageManager = getPackageManager();

        if (packageManager != null) {
            Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");

            if (activated != null) {
                int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;

                if (versionCode >= 3002850) {
                    facebookurl = "fb://page/1548219792xxxxxx";
                }
            } else {
                facebookurl = FACEBOOK_URL;
            }
        } else {
            facebookurl = FACEBOOK_URL;
        }
    } catch (Exception e) {
        facebookurl = FACEBOOK_URL;
    }
    return facebookurl;
}

Solution 17 - Android

To launch facebook page from your app, let urlString = "fb://page/your_fb_page_id"

To launch facebook messenger let urlString = "fb-messenger://user/your_fb_page_id"

FB page id is usually numeric. To get it, goto Find My FB ID input your profile url, something like www.facebook.com/edgedevstudio then click "Find Numberic ID".

Voila, you now have your fb numeric id. replace "your_fb_page_id" with the generated Numeric ID

 val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
 if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
 startActivity(intent)
          

Solution 18 - Android

Check out my code for opening Facebook specific Page:

//ID initialization
ImageView facebook = findViewById(R.id.facebookID);

facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String facebookId = "fb://page/327031464582675";
            String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";

            try {
               startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
            }catch (Exception e){
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
            }
        }
    });

Solution 19 - Android

I have created a method to open facebook page into facebook app, if app is not existing then opening in chrome

    String socailLink="https://www.facebook.com/kfc";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
    if (facebookUrl == null || facebookUrl.length() == 0) {
        Log.d("facebook Url", " is coming as " + facebookUrl);
        return;
    }
    intent.setData(Uri.parse(facebookUrl));
    startActivity(intent);

Utils.class add these method

public static String getFacebookUrl(FragmentActivity activity, String facebook_url) {
    if (activity == null || activity.isFinishing()) return null;

    PackageManager packageManager = activity.getPackageManager();
    try {
        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
        if (versionCode >= 3002850) { //newer versions of fb app
            Log.d("facebook api", "new");
            return "fb://facewebmodal/f?href=" + facebook_url;
        } else { //older versions of fb app
            Log.d("facebook api", "old");
            return "fb://page/" + splitUrl(activity, facebook_url);
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("facebook api", "exception");
        return facebook_url; //normal web url
    }
}

and this

 /***
 * this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
 * @param context contain context
 * @param url contains facebook url like https://www.facebook.com/kfc
 * @return if it successfully split then return "kfc"
 *
 * if exception in splitting then return "https://www.facebook.com/kfc"
 *
 */
 public static String splitUrl(Context context, String url) {
    if (context == null) return null;
    Log.d("Split string: ", url + " ");
    try {
        String splittedUrl[] = url.split(".com/");
        Log.d("Split string: ", splittedUrl[1] + " ");
        return splittedUrl.length == 2 ? splittedUrl[1] : url;
    } catch (Exception ex) {
        return url;
    }
}

Solution 20 - Android

open fb on button click event without using facebook sdk

 Intent FBIntent = new Intent(Intent.ACTION_SEND);
    FBIntent.setType("text/plain");
    FBIntent.setPackage("com.facebook.katana");
    FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
    try {
        context.startActivity(FBIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
    }

Solution 21 - Android

1-to get your id go to your image profile and click right click and take copy link adress.

        try {
                Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/id"));
                startActivity(intent);
            } catch(Exception e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("facebook url")));
            }
        }
    });

Solution 22 - Android

Declare constants

  private String FACEBOOK_URL="https://www.facebook.com/approids";
    private String FACEBOOK_PAGE_ID="approids";

Declare Method

public String getFacebookPageURL(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;

            boolean activated =  packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
            if(activated){
                if ((versionCode >= 3002850)) {
                    Log.d("main","fb first url");
                    return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                } else {
                    return "fb://page/" + FACEBOOK_PAGE_ID;
                }
            }else{
                return FACEBOOK_URL;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL;
        }
    }

Call Function

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                String facebookUrl = getFacebookPageURL(MainActivity.this);
                facebookIntent.setData(Uri.parse(facebookUrl));
                startActivity(facebookIntent);

Solution 23 - Android

You can check it for all Facebook Apps that any of Facebook apps are installed or not . For supporting OS level 11 we need to add this in AndrodiManifest.xml to avoid package name not found exception -

<manifest ...
<queries>
    <package android:name="com.facebook.katana" />
    <package android:name="com.facebook.lite" />
    <package android:name="com.facebook.android" />
    <package android:name="com.example.facebook" />
</queries>
 <application .....

Then add this method to you code -

public static String isFacebookAppInstalled(Context context){

        if(context!=null) {
            PackageManager pm=context.getPackageManager();
            ApplicationInfo applicationInfo;

            //First check that if the main app of facebook is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
                return applicationInfo.enabled?"com.facebook.katana":"";
            } catch (Exception ignored) {
            }

            //Then check that if the facebook lite is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
                return applicationInfo.enabled?"com.facebook.lite":"";
            } catch (Exception ignored) {
            }

            //Then check the other facebook app using different package name is installed or not
            try {
                applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
                return applicationInfo.enabled?"com.facebook.android":"";
            } catch (Exception ignored) {
            }

            try {
                applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
                return applicationInfo.enabled?"com.example.facebook":"";
            } catch (Exception ignored) {
            }
        }
        return "";
    }

And then launch the app -

if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
 /* Facebook App is installed,So launch it. 
  It will return you installed facebook app's package
  name which will be useful to launch the app */

  Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
 Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
                if (intent != null) {
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                else {
                    Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
                    context.startActivity(intentForOtherApp);
                } 
 }

Solution 24 - Android

Answering this in October 2018. The working code is the one using the pageID. I just tested it and it is functional.

public static void openUrl(Context ctx, String url){
    Uri uri = Uri.parse(url);
    if (url.contains(("facebook"))){
        try {
            ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("com.facebook.katana", 0);
            if (applicationInfo.enabled) {
                uri = Uri.parse("fb://page/<page_id>");
                openURI(ctx, uri);
                return;
            }
        } catch (PackageManager.NameNotFoundException ignored) {
            openURI(ctx, uri);
            return;
        }
    }

Solution 25 - Android

I implemented in this form in webview using fragment- inside oncreate:

 webView.setWebViewClient(new WebViewClient()
{
    public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
                {
     if(Uri.parse(urlx).getHost().endsWith("facebook.com")) {
                        {
                            goToFacebook();
                        }
                        return false;
                    }
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                    viewx.getContext().startActivity(intent);
                    return true;
                }

});

and outside onCreateView :

 private void goToFacebook() {
        try {
            String facebookUrl = getFacebookPageURL();
            Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
            facebookIntent.setData(Uri.parse(facebookUrl));
            startActivity(facebookIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //facebook url load
    private String getFacebookPageURL() {
        String FACEBOOK_URL = "https://www.facebook.com/pg/XXpagenameXX/";
        String facebookurl = null;

        try {
            PackageManager packageManager = getActivity().getPackageManager();

            if (packageManager != null) {
                Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");

                if (activated != null) {
                    int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;

                    if (versionCode >= 3002850) {
                        facebookurl = "fb://page/XXXXXXpage_id";
                    }
                } else {
                    facebookurl = FACEBOOK_URL;
                }
            } else {
                facebookurl = FACEBOOK_URL;
            }
        } catch (Exception e) {
            facebookurl = FACEBOOK_URL;
        }
        return facebookurl;
    }

Solution 26 - Android

fun getOpenFacebookIntent(context: Context, url: String) {
    return try {
        context.packageManager.getPackageInfo("com.facebook.katana", 0)
        context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/$url/")))
    } catch (e: Exception) {
        context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
    }
}

Solution 27 - Android

Answer worked for me but one missing thing was to add query in manifest, here is my solution:

private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";

try {
    int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;

    if(!facebookID.isEmpty()) {
        // open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
        Uri uri = Uri.parse("fb://page/" + facebookID);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    } else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
        // open Facebook app using facebook url
        Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    } else {
        // Facebook is not installed. Open the browser
        Uri uri = Uri.parse(facebookUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }
} catch (PackageManager.NameNotFoundException e) {
    // Facebook is not installed. Open the browser
    Uri uri = Uri.parse(facebookUrl);
    startActivity(new Intent(Intent.ACTION_VIEW, uri));
}

}

and add that in Manifest outside the application tag

    <queries>
    <package android:name="com.google.android.gm" />
    <package android:name="com.facebook.katana" />
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

Solution 28 - Android

To open the Facebook page in Facebook app from your Webview add this code in your shouldOverrideUrlLoading method

 if (url.startsWith("https://www.facebook.com || Your Facebook page address")) {
				try {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + url)));
				} catch (Exception e2) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
				}
				return true;
			}

Solution 29 - Android

try {
       String[] parts = url.split("//www.facebook.com/profile.php?id=");
       getPackageManager().getPackageInfo("com.facebook.katana", 0);
       startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim()))));
    } catch (Exception e) {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }

Solution 30 - Android

You can open the facebook app on button click as follows:-

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
	this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			startNewActivity("com.facebook.katana");
		}
	});
	
}

public void startNewActivity( String packageName)
{
    Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent != null)
    {
        // we found the activity
        // now start the activity
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    else
    {
        // bring user to the market
        // or let them choose an app?
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("market://details?id="+packageName));
        startActivity(intent);
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBj&#246;rn MarschollekView Question on Stackoverflow
Solution 1 - AndroidjoaomgcdView Answer on Stackoverflow
Solution 2 - AndroidJared RummlerView Answer on Stackoverflow
Solution 3 - AndroidJohnView Answer on Stackoverflow
Solution 4 - AndroidMurmelView Answer on Stackoverflow
Solution 5 - AndroidSomeguy123View Answer on Stackoverflow
Solution 6 - AndroidMBHView Answer on Stackoverflow
Solution 7 - AndroidViral PatelView Answer on Stackoverflow
Solution 8 - AndroidJorgesysView Answer on Stackoverflow
Solution 9 - AndroidJon ColversonView Answer on Stackoverflow
Solution 10 - AndroidRavi MakvanaView Answer on Stackoverflow
Solution 11 - AndroidSpiralDevView Answer on Stackoverflow
Solution 12 - AndroidJavier Amor PenasView Answer on Stackoverflow
Solution 13 - AndroidigordcView Answer on Stackoverflow
Solution 14 - AndroidShahid AhmadView Answer on Stackoverflow
Solution 15 - AndroidTiger developerView Answer on Stackoverflow
Solution 16 - AndroidImmyView Answer on Stackoverflow
Solution 17 - AndroidLekeOpeView Answer on Stackoverflow
Solution 18 - AndroidPapelView Answer on Stackoverflow
Solution 19 - AndroidAbdul RizwanView Answer on Stackoverflow
Solution 20 - AndroidDrashti VyasView Answer on Stackoverflow
Solution 21 - Androidmohamed gemyView Answer on Stackoverflow
Solution 22 - AndroidAbhishek AgarwalView Answer on Stackoverflow
Solution 23 - AndroidGk Mohammad EmonView Answer on Stackoverflow
Solution 24 - AndroidfranzeszView Answer on Stackoverflow
Solution 25 - AndroidthinktechtvView Answer on Stackoverflow
Solution 26 - AndroidHardik PatelView Answer on Stackoverflow
Solution 27 - AndroidShahid GhafoorView Answer on Stackoverflow
Solution 28 - AndroidHaris KhanView Answer on Stackoverflow
Solution 29 - AndroidJohnView Answer on Stackoverflow
Solution 30 - AndroidDeepak SharmaView Answer on Stackoverflow