Android Question find/link phone numbers or email addresses in text?

Dave O

Well-Known Member
Licensed User
Longtime User
Hi there,

Looking for the best way to auto-detect contact info in a chunk of text, so I can turn it into a clickable link.

I know it's possible, because the Tasks app that I use on my phone already does this. For example, if I type in some text in a task note field, when I later view it, it has turned the phone numbers and email addresses into links tied to appropriate intents:

upload_2016-6-9_12-6-14.png


I'm wondering if this is a Google API, a library, or just some snazzy code they wrote.

I imagine it involves parsing the text for certain formats (using something like GREP?), then turning them into links (with something like webview?).

Anyone done something like this?

Thanks!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Thanks. After reading that, I found another thread that used the SetAutoLinkMask method, and that seemed easier, so I created a function that I can reuse. Hopefully it's useful to others too. I'm thrilled that I can add this feature with a couple lines of code. :)

Here's the Reflection version:
B4X:
'For the given text view (e.g. label), turn on its "linkify" feature (auto-converts text to clickable links).
'After that, you can set the view's text property to the actual text, and the auto-linking will be done.
'Mask determines which types of text to look for:
'0 = no links
'1 = web addresses only
'2 = email addresses only
'4 = phone numbers only
'8 = map addresses only (only works for addresses in USA - see Android's "findAddress" docs)
'15 = all (phone numbers, email addresses, web addresses, map addresses)
'You can combine two settings using bit.or (e.g. for web and email, use bit.or(1, 2))
Sub linkifyTextView(textViewArg As View, maskArg As Int)
  Dim r As Reflector
  r.Target = textViewArg
  r.RunMethod2("setAutoLinkMask", "maskArg", "java.lang.int")
End Sub

...and here's the equivalent JavaObject version:
B4X:
'For the given text view (e.g. label), turn on its "linkify" feature (auto-converts text to clickable links).
'After that, you can set the view's text property to the actual text, and the auto-linking will be done.
'Mask determines which types of text to look for:
'1 = web addresses only
'2 = email addresses only
'4 = phone numbers only
'8 = map addresses only (only works for addresses in USA - see Android's "findAddress" docs)
'15 = all (phone numbers, email addresses, web addresses, map addresses)
'You can combine two settings using bit.or (e.g. for web and email, use bit.or(1, 2))
Sub linkifyTextView(textViewArg As View, maskArg As Int)
   Dim jTextView As JavaObject = textViewArg
   jTextView.RunMethod("setAutoLinkMask", Array As Object(maskArg))
End Sub

Both can be called using code like this:
B4X:
linkifyTextView(bigLabel, 15)
bigLabel.Text = "phone = +1 416 123 4567, email = test@test.com, etc."
 
Last edited:
Upvote 0
Top