Android Linkify - Clickable Phone Numbers

So, I'm trying to add functionality that, when you click on the phone number, will lead you to the Dialer application with a 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 result is only that the specified line cannot be pressed. I even tried adding the following line, but no luck:

mContactDetailsText.setAutoLinkMask(0);

Has anyone got any ideas or can see what I'm doing wrong?

thank

+4
source share
3 answers

The auto-complete mask should include a search for phone numbers:

mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);

Then you will need to configure click links:

mContactDetailsText.setLinksClickable(true);

, :

mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())
+9

, , , , .

 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());
+2

You need to install onClickListener()in TextViews. Then they will respond to clicks.

0
source

All Articles