androidxamarintextviewmailtoautolink

Subject is always empty when I do click on an email in Android


I'm developing an App and I added a simple TextView with autoLink="email"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:textSize="16dp"
    android:autoLink="email"
    android:text="@string/lblContactUs" />

My string looks like this:

<string name="lblContactUs">Federico Navarrete <a href="mailto:fanm_45@outlook.com?Subject=Contact%20Us%TipSal" target="_top">fanm_45@outlook.com</a></string>

Always, when I do click in the link the subject is empty.

Empty subject

Also, I noticed that if I don't have a real email inside of the tag:

<a href="mailto:fanm_45@outlook.com?Subject=Contact%20Us%TipSal">fanm_45@outlook.com</a>

However, I have something like this:

<a href="mailto:fanm_45@outlook.com?Subject=Contact%20Us%TipSal">Contact us</a>

The link doesn't do anything, the code is fully ignored. Does anyone have any idea what should I change? Or why is not working?

PS: I already tested on Gmail and Blue Mail clients, I got the same result.


Solution

  • I think textview can not identify this <a href="mailto:"/>. But textView can identify mail address.

    You can change your string.xml to

    <string name="lblContactUs">fanm_45@outlook.com</string>

    The behavior should be the same with

    <string name="lblContactUs"><a href="mailto:">fanm_45@outlook.com</a></string>

    To achieve your requirement you should use the customer span for mail sending.

    1. Set your text can be clicked by usiing ClickableSpan

    class MyURLSpan : ClickableSpan
    {
        MainActivity mActivity;
    
        public MyURLSpan(MainActivity activity)
        {
            mActivity = activity;
        }
        public override void OnClick(View widget)
        {
            Intent email = new Intent(Intent.ActionSend);
            email.SetType("text/plain"); 
            //real device please use  email.SetType("message/rfc822");
            email.PutExtra(Intent.ExtraEmail, "mikexxma@outlook.com");  
            email.PutExtra(Intent.ExtraSubject, "hello");    
            email.PutExtra(Intent.ExtraText, "hello mike ma");
            mActivity.StartActivity(email);
        }
    }
    

    2. Add the click listener to the text:

    private SpannableString getClickableSpan()
    {
        string s = "contact me";
        SpannableString sp = new SpannableString(s);
        sp.SetSpan(new MyURLSpan(this), 0, s.Length, SpanTypes.InclusiveInclusive);
        return sp;
    }
    

    3. Set the span to the textview :

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    
        // Set our view from the "main" layout resource
         SetContentView (Resource.Layout.Main);
        mailTV = (TextView)FindViewById(Resource.Id.textView2);
        mailTV.SetText(getClickableSpan(), TextView.BufferType.Spannable);         
        mailTV.MovementMethod = LinkMovementMethod.Instance;
    }
    

    You can find :

    enter image description here