How to make Share Sheet in Android

Oğuzhan Aslan
4 min readJun 26, 2021
share sheet in Android 29 (From Official Docs)

When it comes to Share Sheet , newbies generally have confusion. However, the situation is more simple than most of the topics in Android.In this article, I will explain Share Sheets in Android briefly .

In the official article of google, it is stated that.

Android uses Intents and their associated extras to allow users to share information quickly and easily, using their favorite apps.

So building or opening a Share Sheet is not so different than opening another Activity from an Activity. The difference here is instead of passing the Activity classes to Intent, we just putExtra(), action and type to the Intent.

Before giving the code examples about Share Sheet, let’s see the possible types . As, it said in the offical docs:

You should provide the most specific MIME type for the data you’re sending

By the way, If you do not know what is MIME type. It is simply, Multipurpose Internet Mail Extensions (MIME). It is an Internet standard that extends the format of email messages to support text in character sets other than ASCII, as well as attachments of audio, video, images, and application programs.(for further information, please check here .)

Types that we can use are generally :

Text : for simple strings ,or in other words texts, we use “text/plain” as the type as the type of the Intent that we will use for the Share Sheet.

Optionally, we can use other text “type”s which are

text/rtf : for RTF formated texts.

text/html : for Html formated texts. ( yeah this one so obvious :D )

text/json : for Json formated texts

However, you should keep this in mind. The app that you try to send data (or text in this case) must be registered to “text/*” type to receive the data. Of course, most of the advanced apps like Whatsapp are mostly handles these kind of registrations.

Binary data : for binary data instances, like images or videos ,we use one of :

image/jpg : For jpg image files.

image/png : For png files.

image/gif : For gif files.

video/mp4 : For mp4 video files

video/3gp : For 3gp video files. (this one is not so common, I believe. Since I learned it while writing this blog )

Also, receiver apps should be registered for image/* type to receive image data. For video files they should be registered for video/* type

( I think you get the general idea of the process until here )

Note : you may use “*/*MIME type to send data but it is not recommended, since it will be no type safe for the receiver apps. Unless the receiver app that can handle generic data types , you should avoid to use it.

So , if you are OK until here, we can start creating some Share Sheet ( finally!) .

Firstly, for any type of sharing we should create an Intent which contains action as “ACTION_SEND” . We will pass this intent to the Intent.createChooser(…) which the returns the main intent to open the Share Sheet.

As you may see in the code above ( It is the same code which is in the google offical docs.). We put the text we want to share with putExtra() method of the Intent . For text extras, we use Intent.EXTRA_TEXT flag which is the one that you will mostly use but there are other flags for text extras such as EXTRA_EMAIL or EXTRA_CC . Again, please check the docs to get more detailed information about them. Moreover, to send the binary data , the first paramether of the putExtra() should be changed with Intent.EXTRA_STREAM .

In addition, If you look at the “createChooser” line, you can see that we pass the intent into the method with a null value. The null paramether here is the optional title parameter for the intent.

Oh right now, you are probably thinking “what is the ACTION_SEND_MULTIPLE “. Do not be stressed about that, it is just other send action but it is used for multiple data.

I hope everything is good for you . Now, you have enough information to create an Share Sheet and send your data but wait, there is one more thing that you may also use that is listening information of sharing. I mean, listening which component is selected by the user. The process is done by using a Broadcast Receiver as we generally use in Android to listen events.

you should pass a pending intent that contains your receiver to register your receiver. Like this :

After that, you can listen the selected ComponentName in onReceive(…) method on yourBroadcast Receiver .

val clickedComponent : ComponentName = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);

Remember that the “intent” in the code above is coming from the paramethers of the method.

All in all, creating Share Sheet in Android is not that complicated. All you need is a quick research about it. Of course, we did not cover everything that you can do with the Sheet. To learn them, to learn in advance about Share Sheet, please check the official documentation.

Love you all.

Stay tune for upcoming blogs.

Take care.

--

--