[Java for Android App Development] bassie1995:
Hello everyone,

While making an app, I wanted to let the user be able to share some content (shameless plug: from a clearable EditText). So, I looked up how to do this.

In this thread, I will share (pun not even intended) with you how to use the Share Intent yourself, so that your users can also quickly involve anyone they know in what they're doing.

There are some things that I am going to assume you've done:

- You have made your base app

- You have a Button done and set up, only needing a method to execute the Share Intent.

Now, let's get to it!

Step 1 - The method:

First, you have to set up the method. As always, give it a nice and descriptive name, so everyone (including you later, believe me that you'll need it) knows what it does.

Of course, this just follows standard syntax. I'm going to name it share, because simple!

So, you'll get this:

private void share() {
// This is the method where we share some content
}

Step 2 - The Intent:

You want to make sure that Android recognises your Intent as being a Share Intent. Of course, because of the flexibility of Intents, this is also easily done. Just declare another parameter, which isn't the usual set of Classes.

My Intent will be called shareIntent.

Like so:

Intent shareIntent = new Intent(android.content.intent.ACTION_SEND);

Step 3 - The type:

When sharing content in Android, you have to let the system know what you're sharing. Naturally, it has to react differently to different types of content. An image can be handled by certain apps, while others can only handle text. Apart from that, the apps have to process the items differently.

This is done by declaring the MIME type, which are unique identifiers for different data types.

Some possible types are text/plain, image/gif and video/mp4.

In this case, we are going to share some text. This is done by adding the following line under the Intent declaration:

shareIntent.setType("text/plain");

With the setType method, you could target specific apps. However, this is obviously not a good idea. If a user doesn't have that app on their phone, it'll cause trouble. Therefore, just use the right type and let the user select the relevant application.

Step 4 - The Content:

Now for the real work. What are you sharing? Of course, text! But you have to give the Intent that text, because it won't just snuff it out by itself.

How do we do this? Simple, again.

Just create a String and use that. Of course, you can hard code a String in the Intent, but it's best to use a variable. After all, you're probably going to be sharing dynamic content.

This is done as follows, though you should really know this by now:

String sharedString = "Hey, we are sharing!";

Step 5 - The extras:

You have to also give the Intent its extra information. For this example, we'll be setting two types of extras: a subject and the body.

This way, there is flexibility. If the user chooses to share the text using an email application, the subject will also be used. If they don't, it won't be used.

This is done by adding the following lines to your sharing method:

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is the subject");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, sharedString);

Step 5 - The Chooser:

Whenever you share something, you see the Dialog pop up for you to select the app you want to use in your sharing the content. This is the next and last step in this guide, and is basically just starting the Intent with a special parameter.

Not every type of media can be shared to every app. When you share certain content, Android looks at what apps have declared themselves candidates for that MIME type.

So, if you are not seeing certain apps appear, check whether the app can work with that MIME type!

Start the Intent like this:

startActivity(Intent.createChooser(shareIntent, "Share via"));

Of course, the title to the chooser doesn't have to be "Share via". It can be any other text or variable (keep this in mind for apps that have translations!).

Facebook doesn't do this right! The facebook app doesn't do sharing right, so it will probably not show up in the list of apps that you might see when following the tutorial. To counteract this, target the app using the specific API.

Have fun sharing, and thanks for reading!

P.S.: Images can be added, should anyone want me to do so.