How to share a text link via text intent?

Hari Krishnan picture Hari Krishnan · Jul 12, 2016 · Viewed 10.6k times · Source

I want to share a text clicking on which the user will navigate to a url via android share intent.

For example:

"Visit google for more info."

In above text, on clicking google, user will be redirected to google.com. I need to achieve something like this instead of showing the user the complete url. What i have tried is making it an html link with href which doesnt work.

So, How should i achieve it?

Answer

Ashish Ranjan picture Ashish Ranjan · Jul 12, 2016

You must be using the type as plain text i.e, :

intent.setType("text/plain");

set the type to html like this :

intent.setType("text/html");

and it'll work.

So, you can use this to share a html text :

String textToShare = "Visit <a href=\"http://www.google.com\">google</a> for more info.";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);

intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "sample");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(textToShare));

startActivity(Intent.createChooser(intent, "Share"));

OUTPUT :

Here's a screenshot from the Gmail app :

Screenshot from Gmail app