How to make an email address clickable?

前端 未结 5 792
-上瘾入骨i
-上瘾入骨i 2020-12-15 03:10

I have some text in my application that says in case you need extra help, please email us and here is the email address, blah, blah.

But I want them to be able to cl

相关标签:
5条回答
  • 2020-12-15 03:30

    You can make your text clickable by using setOnClickListener on the text

    textView.setOnClickListener(new View.OnClickListener());
    

    You can open the email client by creating a new Intent with the ACTION_SEND. Settype, the email address and subject like this:

    Intent emailintent = new Intent(android.content.Intent.ACTION_SEND);
    emailintent.setType("plain/text");
    emailintent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {"mailk@gmail.com" });
    emailintent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    emailintent.putExtra(android.content.Intent.EXTRA_TEXT,"");
    startActivity(Intent.createChooser(emailintent, "Send mail..."));
    
    0 讨论(0)
  • 2020-12-15 03:33

    You need to fire an intent in your onClickListener:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain"); // send email as plain text
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    intent.putExtra(Intent.EXTRA_TEXT, "mail body");
    startActivity(Intent.createChooser(intent, ""));
    
    0 讨论(0)
  • 2020-12-15 03:35

    Please be aware of a little bug from API 24 onwards which makes the accepted solution not work if the local part of the email address has exactly 2 characters like "it@google.com".

    See the issue: https://issuetracker.google.com/issues/64435698

    Allegedly fixed already, but apparently not rolled out yet. (Don't you love it that they know about the issue and don't even bother to update the documentation accordingly? https://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink)

    So unless you're sure that you're not dealing with such 2-letter email addresses, you should rather use the accepted approach from here for the time being:

    TextView to send email when clicked

    Take care to remove the autolink attribute from the TextView then.

    0 讨论(0)
  • 2020-12-15 03:37

    The accepted answer may work for emails but if you want to detect different patterns like emails, contact numbers, weblink and set a separate on click implementations for these patterns I suggest you to use CustomClickableEmailPhoneTextview

    Sample Code to use the library.

    CustomPartialyClickableTextview customPartialyClickableTextview= (CustomPartialyClickableTextview) findViewById(R.id.textViewCustom);
    
                    /**
                     * Create Objects For Click Patterns
                     */
                    ClickPattern email=new ClickPattern();
                    ClickPattern phone=new ClickPattern();
                    ClickPattern weblink=new ClickPattern();
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is email
                     */
                    email.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
    
                            Toast.makeText(MainActivity.this,"email clicked",Toast.LENGTH_LONG).show();
    
    
                        }
                    });
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is phone
                     */
                    phone.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
                            Toast.makeText(MainActivity.this,"phone clicked",Toast.LENGTH_LONG).show();
    
                        }
                    });
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is weblink
                     */
                    weblink.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
                            Toast.makeText(MainActivity.this,"website clicked",Toast.LENGTH_LONG).show();
    
                        }
                    });
    
                    /**
                     * set respective regex string to be used to identify patter
                     */
                    email.setRegex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"); // regex for email
                    phone.setRegex("[1-9][0-9]{9,14}"); // regex for phone number
                    weblink.setRegex("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"); // regex for weblink
    
                    /**
                     * add click pattern to the custom textview - first parameter is tag for reference second parameter is ClickPattern object
                     */
                    customPartialyClickableTextview.addClickPattern("email",email);
                    customPartialyClickableTextview.addClickPattern("phone",phone);
                    customPartialyClickableTextview.addClickPattern("weblink",weblink);
    
    0 讨论(0)
  • 2020-12-15 03:38

    This is a very reasonable request and the Linkify class will turn every email address into an appropriate link for you. Simply add the autoLink attribute to your XML:

    <TextView
        ...
        android:autoLink="email" />
    
    0 讨论(0)
提交回复
热议问题