42

When I am using android on websites and reading emails, I notice that I can click on addresses to load into google maps, or click on phone numbers to call, or click on emails and send an email.

These elements on the web are formatted in a variety of ways, so there is some built in function that detects these sort of things.

How do I allow this within my app? I have a page which displays contact information in plain text and I would like the user to just be able to click.

Do I Absolutely need to create clicklisteners for each textview or is there a system function I just need to enable?

CQM
  • 42,592
  • 75
  • 224
  • 366

6 Answers6

77

Use

android:autoLink="phone"

in textView in the xml layout file

luckyhandler
  • 10,651
  • 3
  • 47
  • 64
Amt87
  • 5,493
  • 4
  • 32
  • 52
39

Android has a utility expressly for this purpose: Linkify

TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);

See also: https://android-developers.googleblog.com/2008/03/linkify-your-text.html

mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
zienkikk
  • 2,404
  • 1
  • 21
  • 28
  • 17
    You could edit the answer to be more clear what Linkifi is. My first thought was, this is a 3rd party lib and i automatically skipped your answer. But Linkify is really the ultimate solution! :) – Primoz990 Feb 04 '16 at 10:53
24
import android.text.util.Linkify;

Linkify.addLinks(text, Linkify.PHONE_NUMBERS);
Keith Maurino
  • 3,374
  • 10
  • 40
  • 48
6

You can use it in TextView like this,

Set android:autoLink="phone" as below,

<TextView
    android:layout_width="fill_parent"
    android:id="@+id/text"
    android:layout_height="wrap_content"
    android:autoLink="phone"
    android:gravity="center"
    android:linksClickable="true"
    android:text="@string/txtCredits" />

However,

For some reason above code does not work all time. So, add below code also,

TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Yuvraj Pandey
  • 101
  • 1
  • 6
4
android:autoLink="phone"

was working for me on all phones... except Samsung. Therefore, I chose the following option. Transformed phone number texts to support click to call:

<a href="tel:+4930123456789">+49 / 30 123456789</a>

and then used this static helper method to add web link support to my TextViews

public static void linkifyTextViews(@NonNull TextView... textViews) {
    for (TextView textView : textViews) {
        Linkify.addLinks(textView, Linkify.WEB_URLS);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
luckyhandler
  • 10,651
  • 3
  • 47
  • 64
0

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

enter image description here

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);
Nitin Jaiman
  • 173
  • 1
  • 12