Luz Gonzalez Luz Gonzalez - 12 days ago 6
Android Question

Getting a clickable URL from strings.xml for a share Intent Android Studio

I've been searching for hours trying to get my share Intent to be a hyperlink to a specific URL. I saw a lot of answers for this for a TextView but none for a sharing intent and I couldn't figure out how to get the textView answers applied to this.

Basically I want this string

<string name="link"><a href="https://play.google.com/store/apps/details?blahblah">EatingOut</a></string>


to work with

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant);
share = getString(R.string.link);
}

private void setShareIntent() {

if (mShareActionProvider != null) {

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Share");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Eating at "+ title.getText() + " #" + share );

mShareActionProvider.setShareIntent(shareIntent);
}
}


The problem is when I share it, there is no hyperlink to the "share" part.
I have tried many different suggestions I read here but like I said most were for textViews

edit: tried the TextUtils.concat but still no hyperlink with and without "#"

CharSequence socialShare = TextUtils.concat("Eating at ", title.getText(), " #", share);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Share");
shareIntent.putExtra(Intent.EXTRA_TEXT, socialShare );

Answer

The problem is when I share it, there is no hyperlink to the "share" part.

You have a few problems here.

First, I am not aware that <a> is a valid HTML-style tag that you can use in a string resource.

Second, getString() will remove that HTML-style markup, even if it exists.

Third, you are setting the MIME type of your ACTION_SEND Intent to text/plain, and HTML is a different MIME type.


Given all that, you need to decide whether you are going to share plain text with a URL just in the text (relying upon the "share" recipient to identify the URL and make it useful to the user, if applicable), or if you are going to share HTML (and hope that the "share" recipient doesn't screw it up too bad).

If you are going to share plain text, then change your string resource to:

<string name="link">https://play.google.com/store/apps/details?blahblah</string>

You will also want to either get rid of the # in your concatenated string, or add a space after it, as putting # right before the URL may confuse other apps, causing them to not recognize the URL as being a URL.

If you are going to share HTML, you need to change setType() to pass text/html, then set EXTRA_TEXT to be either an HTML-formatted string or a Spanned object (e.g., created via SpannableStringBuilder).

Comments