Java – How to open a profile on the Facebook app from an Android app

How to open a profile on the Facebook app from an Android app… here is a solution to the problem.

How to open a profile on the Facebook app from an Android app

I’m trying to set up a click listener on ImageView, which will open the Facebook app on a specific page.
The variable “facebook” contains the URL of the Facebook page I want to open
My click listener code is as follows:

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

if (facebook != null) {
                        Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                        String facebookUrl = getFacebookPageURL(context);
                        facebookIntent.setData(Uri.parse(facebookUrl));
                        startActivity(facebookIntent);
                    }
                }
            });

The getFacebookUrl() method is as follows:

//Open club's Facebook page
        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;
                } else { //older versions of fb app
                    return "fb://page/" + facebook;
                }
            } catch (PackageManager.NameNotFoundException e) {
                return facebook; normal web url
            }
        }

This code doesn’t work for me. I have a null pointer exception on the following two lines:
PackageManager packageManager = context.getPackageManager();

String facebookUrl = getFacebookPageURL(context);

I think it’s related to context variables. I didn’t know much about it and wasn’t sure exactly what to put it, so I used Open Facebook Page in Facebook App (if installed) on Android code. Can anyone help me with this?

Solution

I don’t think you declare your context in the body, you have to add,

context=getApplicationContext(); 

or

context=YourActivity.this;

Your body.

This worked for me.

Button btn;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn=(Button)findViewById(R.id.btn);
    context=getApplicationContext();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isAppInstalled()) {
                Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
                Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                String facebookUrl = getFacebookPageURL(context);
                facebookIntent.setData(Uri.parse(facebookUrl));
                startActivity(facebookIntent);

} else {
                Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
            }

}
    });
}
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.orca", 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
    }
}

public boolean isAppInstalled() {
    try {
        getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Related Problems and Solutions