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.