Using the Share Dialog

If you currently have a share function in your app you are most likely using the default share dialog provided by the Android platform.

The Loopy ™ Share Dialog mimics the default Android share dialog but provides important callbacks to allow more sophisticated social analytics.

To implement the Loopy ™ share dialog, simply use the showShareDialog method to create a trackable URL for sharing and display the default share dialog.

images/share_dialog.png

If you don’t already have one, add a button to your layout:

<Button
    android:id="@+id/btnShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Share"/>

Add an onClick event in your Activity:

final Activity context = this;

// This would be the url you are wanting to share...
final String urlToShare = "http://www.sharethis.com";   // <== TODO: Set this

// Set the content type for the intent (*/* means ALL)
final String contentType = "*/*";                       // <== TODO: Set this

// Set the title for the dialog
final String dialogTitle = "Share to...";               // <== TODO: Set this

// Create an intent.  The URL to be shared will be available in the callback below
final Intent shareIntent = new Intent(Intent.ACTION_SEND);

// Set the conent type for the intent (*/* means ALL)
shareIntent.setType(contentType);

// Grab a reference to your share button
Button shareButton = (Button) findViewById(R.id.btnShare);

// Trigger the share dialog on click
shareButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // Call the "showShareDialog" endpoint to render the share dialog
        // The listener is used to set the trackable URL on to the share intent.
        Loopy.showShareDialog(context, "Share to...", urlToShare, shareIntent, new ShareDialogListener() {

            @Override
            public void onLinkGenerated(Item item, Throwable error) {
                if (error == null) {
                    // We got a tracking shortlink ok, set it in the body of the share (or wherever you like)
                    shareIntent.putExtra(Intent.EXTRA_TEXT, item.getShortlink());
                } else {
                    // We couldn't get the shortlink, just use the original URL
                    shareIntent.putExtra(Intent.EXTRA_TEXT, urlToShare);
                }
            }
        });
    }
});