5

So I am trying to add the functionality that when you click on a phone number it would take you to the Dialer app with the pre-populated number. I have the code below:

mContactDetailsText.setText(phonetextBuilder.toString());
            Pattern pattern = Pattern.compile("[0-9]+\\s+[0-9]+");
            Linkify.addLinks(mContactDetailsText, pattern, "tel:");

and the Text is currently "T. 0123 4567890"

The current outcome is just having the above string without it being clickable. I have even tried added the following line, but to no luck:

mContactDetailsText.setAutoLinkMask(0);

Anyone got any ideas or can see what I am doing wrong?

Thanks

James King
  • 2,425
  • 7
  • 30
  • 45

3 Answers3

10

The autolink mask needs to include a search for phone numbers:

mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);

Then you'll need to set the links to be clickable:

mContactDetailsText.setLinksClickable(true);

You might also need movement method set like so:

mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())
John Cummings
  • 1,949
  • 3
  • 22
  • 38
  • On Android 5+ this doesn't work even though the pattern matched and highlight phone numbers correctly on pre-lollipop. Do you know why? – user802421 Jul 08 '15 at 07:37
  • The code in the answer works for me on both Lollipop and Kit Kat. Without your code, it's hard to know what's going on. – John Cummings Jul 08 '15 at 18:43
4

You should be able to accomplish what you want with the other answers, but this will definitely work and will give you more control over the display of the text and what will happen when you click the number.

 String text = "T. ";
 StringBuilder stringBuilder = new StringBuilder(text);
 int phoneSpanStart = stringBuilder.length();
 String phoneNumber = "0123 4567890"
 stringBuilder.append(phoneNumber);
 int phoneSpanEnd = stringBuilder.length();

 ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
                startActivity(intent); 
            }

            public void updateDrawState(TextPaint ds) {// override updateDrawState
                ds.setUnderlineText(false); // set to false to remove underline
                ds.setColor(Color.BLUE);
            }
        };
   SpannableString spannableString = new SpannableString(stringBuilder);
   spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 myTextView.setText(spannableString);
 myTextView.setMovementMethod(LinkMovementMethod.getInstance());
Nick Pontiff
  • 144
  • 5
0

You need to set an onClickListener() on your TextViews. Then they will respond to clicks.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • Ah of course. Jesus I am tired. However, I thought with Android you could highlight it and/or underline it in blue to show that you are able to click on it? – James King Jan 13 '15 at 17:40
  • I believe Linkify should add underlines to it automatically when TextView is clickable. Try to set android:clickable="true" in your layout xml file. – IgorGanapolsky Jan 13 '15 at 17:50