Sharing from your android app on facebook/other sites

The following code snippet can be used to begin the activity to share from your android app using any applications that support sharing plain text with the intent Intent.ACTION_SEND.  For example, you may want to add a button or a menu item in your android app to allow users to share your app.  In the handler of the button click or the menu item selection, you call the method in the below snippet.  This snippet will start the activity with intent Intent.ACTION_SEND.  The user is then shown a dialog to chose from a list of applications that support this intent.  This list typically includes applications such as Facebook, Twitter and Google+.  Once the user selects one of these apps, the content added using intent.putExtra(Intent.EXTRA_TEXT, "<>"); is shared by the application.

import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;

public class Sharer {
    public static boolean share(final MenuItem item,
        final Activity activity)
    {
     final Intent intent =
            new Intent(Intent.ACTION_SEND);
     intent.setType("text/plain");
     intent.putExtra(Intent.EXTRA_TEXT,
            "Check out the android app  "
                + "");
     activity.startActivity(
            Intent.createChooser(intent, "Share with"));
     return true;
    }
}

Here I use the androidzoom.com url, instead of the Google Play url because with Google Play url, I observed that facebook picks wrong images while sharing the app. Whereas with androidzoom.com urls, facebook picks the right images while sharing.

Comments