This tutorial runs through the basic process of creating a share button, on every application usually they have a button to share the content on social media through this button. This process implementing the share Intent, passing the content and building a chooser list to share.
You can share the content across multiple channels, including email, text messaging, social networking and many more. You can give a good user level of control over how they wish to share their content by letting them choose the list of sharing application available on their own devices.
Step 1: Start a New Android Project
I assumed that you have already installed your IDE and integrated the SDK android. Then you just need to create new project from your IDE. This how you can achieve from Eclipse, choose “File,” “New,” “Project” then “Android Project.” Fill in your project details and click “Finish.” your new app’s details will appear in the workspace.
Step 2: Choose an Activity to Launch the Share Intent
If you already have an Activity in your Android project that you plan on launching the share Intent from, open it in your IDE’s editor area. Otherwise, you can use the main class for your new app or create a new class, making it an Activity so that you can include a button to launch your sharing Intent. Make your class extend the Activity class and include an “onCreate” method in which you can build your user interface elements.
Step 3: Create a Share Button
For this step you can use kind of button widget that are provided on Andoid, even you can use ImageButton or ImageView. It depend on what you need to use. You can launch your sharing Intent on any user action, such as pressing an options or context menu item. The following Java code creates a regular button, inside the “onCreate” method of the Activity or “onCreateView” method on the Fragment:
This creates a button based on the Android Button class, which the Activity will need to import. This code refers to implement the “setOnclickListener” method. so this is the method to listening on what you want to do on your button. This code specifies a method for the application to call when users press the share button. By including the code in a dedicated method, you can call on the sharing functionality from multiple locations in your code, such as from the “onContextItemSelected” method for long-presses or the “onOptionsItemSelected” for option menu button presses.
Step 4: Implement the Share Method
Add a new method on your activity, make sure to matching the name in your button listener method, as follows:
You can passing the value on it. This method will contain the implementation code for sharing content from your app. You can choose to pass parameters to the method if this suits your project.
Step 5: Create a Send Intent
Create the sharing Intent. Add the following Java code inside your sharing method, creating an Intent object with the send action type:
Step 6: Set the Sharing Type
Set a MIME type for the content you’re sharing. This will determine which applications the chooser list presents to users. Plain text, HTML, images and videos are among the common types to share. The following Java code demonstrates sending plain text:
For more information about what other type that you can use, refers to this (link) and here is the following official information (link). It is possible to target specific applications using the “setType” method, but this can be a risky strategy, potentially causing problems if the user does not have those particular apps installed. So it will be the your decision to set a chooser.
Step 7: Build the Share Content
You can pass various content that you want to share on Intent, including subject, text / media content, and addresses to copy to in the case of email sharing. Following this Java code:
You can of course build the content using variables and methods within your application. Change to what value you want to share in variable.
Step 8: Pass Content to the Intent
Pass the sharing content with “putExtras” method of Intent using this following Java code:
The first line from that image is to set a subject when you want to share it with Email Application like Gmail yahoo! and other application that have subject on it.
Step 9: Create a Chooser
So from this step, you have complete to set the contents that you want to share and you simply have instruct the Android to let the user choose their sharing medium. For the last step, you need to start the Intent with this following Java code:
This code passes the name of the sharing Intent along with a title to display at the top of the chooser list. This example uses “Share via” which is a standard option you may have seen in existing apps. However, you can choose a title to suit your own application.
And this is what I have done on my application.