6

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?

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55

3 Answers3

11

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

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
2

Visit google

Use a href tag and set Text here with Html.fromHtml(); and set to textview

Piyush
  • 18,895
  • 5
  • 32
  • 63
JAAD
  • 12,349
  • 7
  • 36
  • 57
-1

try like this:

use text view selection and long press listener to call share intent. otherwise you can use android spannable textview.

Refer this link for spannable textview with google search: http://androidcocktail.blogspot.in/2014/03/android-spannablestring-example.html

Uri uri = Uri.parse("http://www.google.com/#q=selected word here");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Sackurise
  • 2,804
  • 1
  • 18
  • 18