I bet you had a moment when you needed to share something from your app. And if you didn’t, trust me, you’ll get there eventually.
Regardless of the reason, there are cases, where you’d like to filter out the apps, that will be displayed in share dialog. Or you’d like to write custom dialog. Anyway, you’d like to have some control over the sharing dialog behaviour.
If that’s the case, you’re in the right place! 🙂
Show me the code!
Yep, you’re here for the code (as we all know, the Internet is for code, right? ;))
The most basic sharing intent code would look something like this:
Above code will show standard share dialog allowing user to pick the appropriate app. What if you need to filter out the apps or share to a distinct application (that e.g. don’t have the SDK like Facebook or Twitter)? Let’s say we’d like to check if a given application is installed. There are cases, when you need this kind of information. Having in mind, that Android has a very strict rule, that package name can’t be changed for the application, makes it possible to map the application in question to the package. To find the package name for the app, it’s enough to search for it in Google Play on your browser.
Here we see, that package name for twitter is
com.twitter.android. Let’s see the code, that will go through all installed apps on the device and check if given packages are avaliable. For further usage, we’ll write it in a way, that it returns list of matching
Having this, we can introduce a method to check if the listed packages are installed:
Methods are prepared in a way to pass several packages, as it’s sometimes easier to use. So, we checked that our app is installed; how can we make it, so our
Intent will be sent to a specific app?
There are two ways to do so. If we have only one target app, we should use
Intent.setPackage. On the other hand, if you’d like to show a picker prioritizing given apps, you can use
Having in mind the previous method implementented, we can do it like that:
Those are the basics of how you can customize your sharing functionality.
Using the code from
Intent.setPackage method you can write a completely custom share view.
For more info please check our github sample project, containing really simple custom share view. That’s it for now.
Feel free to contact me if you find the post useful or you have some feedback regarding the content (especially if you find a bug in my sample project ;))