How to add email link to layout xml, Android

2019-02-18 19:24发布

问题:

I have an xml file I use to show the About AlertDialog in my App. I was able to have an area of text from a text resource, and an image to the right of the text. Now I need to add the support email and possible home website address to the text. But I need it to be clickable. So clicking on the email will send an email and clicking on the website will open the browser. How do I add those linked text?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weightSum="1" android:orientation="horizontal" android:baselineAligned="true">
    <TextView android:text="@string/AboutString" android:layout_gravity="center_horizontal" android:id="@+id/textView1" android:layout_height="173dp" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:drawableRight="@drawable/explain"></TextView>
</LinearLayout>

EDIT: The solution I chose:

                View layout = inflater.inflate(R.layout.about, null);
                Pattern p = Pattern.compile("somemail@domain.net");
                String Scheme = "mailto:somemail@domain.net";
                Linkify.addLinks((TextView)layout.findViewById(R.id.textView1), p, Scheme);

回答1:

Check out this article. You need to linkify your text.



回答2:

You need something like this shameless plug: https://github.com/ariefbayu/Clickable-URL-TextView-Example

Basically, you would set your TextView into:

        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:clickable="true" />

on onCreate:

html.append("<a href='lauch.TCActivity://SENDEMAIL?email=mail@example.com&subject=email subject&body=this is email body'>Email</a>");
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

And process it in TCActivity:

    if(data.getHost().equals("SENDEMAIL")){
        Log.i("LOG", "Email:" + data.getQueryParameter("email"));
        Log.i("LOG", "Subject:" + data.getQueryParameter("subject"));
        Log.i("LOG", "Body:" + data.getQueryParameter("body"));
    }

That's it. You should now get the Idea of what to do next.

Clue:

  • http://android-er.blogspot.com/2011/03/send-email-using-intentactionsend.html


回答3:

This was simple for me (using SDK version 23. Not sure if this property exists in earlier version). Add autoLink="email" in textview and give email id in Strings.xml. This email id comes in blue color as link and it opens us email once clicked. Pls try and let me know.

<string name="MailText">Click abc@gmail.com to send us a mail for any query</string>

<TextView
    android:id="@+id/mailtext"
    android:text="@string/MailText"
    android:autoLink="email"
    android:layout_height="wrap_content" />