Search code examples
androidtextview

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 [email protected].\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 [email protected].\nblah blah blah."</string>
    

    Finally

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