Sunday, 9 October 2011

Adding a Share Button to Your Android Apps

The share button and list for one of my apps.

I recently approached what I assumed would be the simple task of adding a share button to my Android apps. While the implementation is indeed simple, I was disappointed, yet again, by the lack of easily digestible guidance online, for what must be a very common task.

Anyway, for anyone in the same boat, here's what I learned. For my purposes I only needed a very basic implementation, so it may not be sufficient for your own applications - it should give you the generic outline in any case. For more specific guidance see my Mobiletuts+ tutorial - Android SDK: Implement a Share Intent on the subject.

Share Intent

To create a share button you have to start a new Intent, passing it the SEND action. When the Activity for your SEND Intent starts, it will present the user with a list of possible channels to share the content through. What each user sees will depend on their own device setup, but it will typically include email, Gmail, Twitter, Facebook, text messaging etc. You will almost certainly have seen the share button in action as an Android user, for example in the browser program.

Here is a basic outline of the code involved in instantiating a share button with the chooser list:

//create the send intent
Intent shareIntent = 
 new Intent(android.content.Intent.ACTION_SEND);

//set the type
shareIntent.setType("text/plain");

//add a subject
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
 "Insert Subject Here");

//build the body of the message to be shared
String shareMessage = "Insert message body here.";

//add the message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
 shareMessage);

//start the chooser for sharing
startActivity(Intent.createChooser(shareIntent, 
 "Insert share chooser title here"));


The code first creates an Intent with SEND as the action. The "setType" method allows you to choose a MIME type for the content being shared, such as plain text or HTML. The code here features two calls to the "putExtra" method, through which you can set various elements of your share item, subject and text content in this case. Finally, the code starts the Activity using the Intent sharing settings indicated, instructing Android to create a chooser list allowing users to pick a sharing medium.

There are other options within the sharing implementation, and you can obviously alter the parameters, method calls etc. to suit your own apps. What options you pick really depends on what you're sharing.

Considerations

Bear in mind the fact that certain aspects of what you set using "putExtra" will be irrelevant for certain sharing channels. For example, if you choose to set a title like I have, it won't appear if the user opts to share using a text message. Also, if you want your users to share using Twitter, remember the 140 character limit.

It's actually tricky to create sharing content that will suit all of the possible messaging services. If you don't want users to choose from the list of options you can bypass that part of the process, but that means you have to create a button or other user control for each messaging service you want users to choose from.

As well as providing the highest level of choice for your users, in my view it's best to use familiar interaction where possible, and Android users are by now accustomed to the sharing chooser list. The downside is that you need to keep your sharing content pretty simple and standard.

For my apps, I was tempted to include a Twitter hashtag in the share content, since it suits my content, but clearly it would be redundant for users sharing through services other than Twitter. Obviously users can edit the content before going ahead and sending it, but it would be poor practice to include a load of guff the user has to delete each time they want to share something. I've seen some apps in which the developer has crammed a load of publicity text including links etc. in with their sharing content - tempting, but not to be recommended from a usability point of view.

Resources

Hope that proved useful to some people, here are some related resources:

Please feel free to add any of your own tips for sharing in Android apps.

Update November 2011
I've written a tutorial on share buttons for Mobiletuts+ Android SDK: Implement a Share Intent which goes into more detail.

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello
      The list depends on which applications the device has installed which can handle the send intent. You will only see a list if the device has more than one app capable of handling the intent. If only one app can handle it, this app will launch automatically. This is particularly an issue when you run apps on an emulator rather than an actual device.

      Delete
  2. Hi

    I would like to have the Share button in the FACEBOOK application how to get it

    ReplyDelete
    Replies
    1. Hi Naveen
      If the device (or AVD) you are running your app on has Facebook installed it should come up on the share list for this code.

      Delete
  3. Perfect!!! Thank you!!!

    ReplyDelete
  4. When I try to share it with Facebook, no content (as subject, message body, ..) are used! It only opens a Facebook Activity where I can write my new state! Can you help me, please?

    ReplyDelete
  5. You are a star. That saved me an a lot of time!

    ReplyDelete
  6. How can i share a image? in facebook whatsApp tumblr pinterest etc
    can u please help!!!!

    ReplyDelete
  7. I got this to work, but the shared link opens Google play on the browser, not the Google play app directly. This get more difficult for the user and will probably not generate much downloads. How can I get the shared link to open the Google play app instead of the browser? This is my code:

    private void shareIt() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "The app");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=morph.galaxytt");
    startActivity(Intent.createChooser(sharingIntent, "Share via"));

    ReplyDelete