简体   繁体   中英

How to make XML string resource email clickable with predefined Subject and Body on TextView

Namaste to all,

I am trying to achieve a solution where on click of email id in strings XML containing HTML tags we should open Intent Chooser of email apps available in the system, then in the email compose screen mailTo , subject & the body should be populated.

 to - recipient@xyz.com subject - xyz.... body - xyz.....

I have tried using android:autoLink="email" attribute, it is making the email clickable but it only pre-populates the recipient's email id and nothing else.

Is there any other way around to achieve the required solution, without using a third-party library?

You have to add a link as follow:

mailto:recipient@xyz.com?subject=xyz subject&body=xyz body

This link will open an Intent for Mail application and will contain a prefill subject and body. (Ref: https://css-tricks.com/snippets/html/mailto-links/ )

You can have a string as follow:

<string name="link_str"><a href="mailto:recipient@xyz.com?subject=xyz subject&body=xyz body">Send email</a>  </string>

And then make TextView linkable as specified in https://stackoverflow.com/a/49694038/5309486

Namaste to all,

Below is the working solution, without using the android:autoLink="email" attribute.

strings.xml

 <string name="str_email_included">Mail us at - &lt;br/> &lt;a href=\'mailto:hello@xyz.com?subject=Subject1&amp;body=Body1\'>hello@xyz.com&lt;/a></string>

Java

 textView.setText(Html.fromHtml(getString(R.string.str_email_included))); textView.setMovementMethod(LinkMovementMethod.getInstance());

Kotlin

 textView.text = HtmlCompat.fromHtml(getString(R.string.str_email_included), HtmlCompat.FROM_HTML_MODE_LEGACY) textView.movementMethod = LinkMovementMethod.getInstance()

The above movementMethod does the job, so onClick of email id, Intent Chooser will open and the mailTo, subject & body get pre-populated as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM