Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android TextView cannot make both link and email address clickable

In my current Android project i have a requirement to display an AlertDialog with a custom layout.

the custom layout resembles this:-

<TextView
    android:id="@+id/dialog_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:textStyle="bold"
    android:autoLink="email"
    android:linksClickable="true"
    android:lineSpacingExtra="8sp"
    android:layout_alignParentTop="true"
    android:textSize="@dimen/body"
    android:layout_marginTop="22dp"
    android:text="@string/dialog_message" />

the dialog_message string resource resembles this:-

<string name="dialog_message">"blah blah blah our <a href="http://www.mywebsite.com">website</a>. email us at myemailaddress@gmail.com.\nblah blah blah."</string>

by using a combination of xml attributes and or code i can either enable the clickable link to my website address or the email address. i cannot get both the link and email address clickable at the same time.

is this not possible.

how do you make both a web address and email address clickable in a single text?
do i have to create two string resources one containing the clickable web address and the other string resource contains the clickable email address?

>Solution :

First, I recommend you to correct the strings as follows.

<string name="dialog_message">"blah blah blah our <![CDATA[<a href="http://www.mywebsite.com">website</a>]]>. email us at myemailaddress@gmail.com.\nblah blah blah."</string>

Finally

dialog_message.setMovementMethod(LinkMovementMethod.getInstance())
dialog_message.setText(Html.fromHtml(getString(R.string.dialog_message)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading