Use the Sharesheet for Wealthy Hyperlink Sharing
Android App Hyperlinks enable your utility to open net hyperlinks as a substitute of an online browser. Dealing with these deep hyperlinks has been lined in our Deep Hyperlinks Crash Course. Along with having the ability to open deep hyperlinks, your app ought to be capable to create and share deep hyperlinks to particular content material as effectively.
Sharing hyperlinks to content material inside your utility is a vital motion in Android. Hyperlink sharing permits your app and its customers to interact in collaboration, communication, social networking, and so forth. All of those actions enhance engagement along with your utility, and provides your utility’s deep linking assist a correct exercise. A fantastic device for sharing linked content material is the Android Sharesheet. This text will present why it is best to use the Sharesheet and show the way to use it with linked content material. Let’s get began!
First, what’s the Sharesheet? Android gives an in-app interface for choosing customers or functions to ship content material on to. This interface is named the Sharesheet, which shows whenever you name Intent.createChooser
utilizing an intent with the ACTION_SEND
motion. The Sharesheet gives an efficient solution to share knowledge with different apps with out utilizing specific intents.
The next instance will invoke the Sharesheet, presenting customers with related targets to share to. Your customers will every have a sheet tailor-made to their put in functions and contacts.
This can be a primary instance, and we’re more likely to be sharing one thing extra fascinating than plain textual content. In case your utility helps Android App Hyperlinks, it might probably share hyperlinks on to content material inside the appliance. Let’s have a look at a pattern hyperlink: “https://instance.com/specials/spaghetti“. This hyperlink is evident to customers of the app that it’s a spaghetti particular, and we may in all probability get by with out spicing up the share sheet.
Nonetheless, typically a hyperlink’s vacation spot won’t be clear. A hyperlink to a Google Doc, for example, usually seems to be one thing like this: https://docs.google.com/doc/d/1TejHHDrz…NotARealLink. From the hyperlink alone, a consumer won’t know what info they’re really sharing. On this case, we should always contemplate including a richer preview.
Coincidentally, that is additionally the steering from the Android documentation https://developer.android.com/coaching/sharing/ship.
Think about sharing a sophisticated URL like:
https://www.google.com/search?ei=2rRVXcLkJajM0PEPoLy7oA4.
A richer preview can reassure your customers what's being shared.
For our use, a richer preview consists of additional title textual content and a picture thumbnail. We will set our title textual content by including an outline to Intent.EXTRA_TITLE
. To embed pictures within the Sharesheet, we set the Intent’s clipData
parameter to a content material URI that’s served by a file-provider. The remainder of this text will stroll you thru offering richer share content material previews within the Sharesheet by embedding pictures from an arbitrary URL.
First: Let’s create a file-provider in our AndroidManifest.xml file.
Now we have to add the filepaths.xml
file to our XML assets listing. For our instance we are going to present pictures from the basis or the cache listing. This instance filepaths.xml
file will hyperlink our cache root to the content material uri for “/pictures”.
Whereas not strictly obligatory, in accordance with the FileProvider API docs we should always create a stub implementation for our utility. Quote, “It’s attainable to make use of FileProvider immediately as a substitute of extending it. Nonetheless, this isn’t dependable and can trigger crashes on some units.”
So what has this really achieved? We’ve declared a file supplier that may make a content material URI for pictures within the root of our cache listing. We will name FileProvider.getUriForFile
to create a URI that Sharesheet can use to show the picture.
With a hyperlink, title, and content material Uri to our picture, we are actually able to make our intent.
And voila! The Sharesheet with wealthy textual content previews.