Android share Intent with clickable link

8.4k views Asked by At

I have looked around the web but have yet to find a solution that fits my specific need. I am looking for a way to share information with a share intent that provides a clickable link, something like:

Check out this news article

via Jimmy's News App

I have successfully set up a share intent in my android app which looks like this:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Text");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Check out this news article" + "\n\n" + getResources().getText(R.string.shared_via));
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "Share this article with..."));

My string resource looks like this:

<string name="shared_via">via <a ref="http://google.com">Jimmy's News App</a></string>

The sharing functions as it should however when shared in an Email, Twitter, etc. the link is ignored and the tweet shows only plain text like this:

enter image description here

I tried playing around with the MIME type, but still no cigar. Is there anyway to get "Jimmy's News App" clickable when shared? I am more than greatful for any and all help and or tips.

Thanks in advance!

1

There are 1 answers

0
CommonsWare On BEST ANSWER

First, I wouldn't have expected your project to even build, as string resources do not support arbitrary HTML tags. The only documented ones are <b>, <i>, and <u>.

Second, even if it does support arbitrary HTML tags, you are converting it back from a Spanned (getText()) into a plain string, which will remove that formatting.

To overcome both problems, either move that string into Java (after all, it's not like you have i18n going, with hardcoded English elsewhere in your code snippet), or wrap the string content in a CDATA (while also fixing your broken HTML, to use href for the <a> attribute):

<string name="shared_via"><![CDATA[via <a href="http://google.com">Jimmy's News App</a>]]></string>

At this point, if you look at your concatenated string, it should look like quasi-HTML source:

Check out this news article

via <a href="http://google.com">Jimmy's News App</a>

Next, while you are sending over HTML, you are declaring it as plain text. Many apps will therefore treat it as plain text, and may do anything from ignoring the tag to showing the raw HTML. You are welcome to try text/html as a MIME type and see if you get better results.

Finally, there is no requirement that any app actually honor your links. ACTION_SEND is a request, not a command. There are no rules for how third-party apps use the HTML that you send over, and so you are going to get varying results from varying apps.