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.

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.

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.

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:

Capture

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.

Add a new method on your activity, make sure to matching the name in your button listener method, as follows:

Capture1

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.

Create the sharing Intent. Add the following Java code inside your sharing method, creating an Intent object with the send action type:

Capture2

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:

Capture3

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.

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:

Capture4

You can of course build the content using variables and methods within your application. Change to what value you want to share in variable.

Pass the sharing content with “putExtras” method of Intent using this following Java code:

Capture5

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.

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:

Capture6

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.

Screenshot_2015-06-27-11-36-28 Screenshot_2015-06-27-11-36-41 Screenshot_2015-06-27-11-37-02

refereces:

code tutplus
android official
code google

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s