22097

Linkify Phone-numbers using Regex

Question:

I am trying to linkify phonenumbers using regular expressions, but i cant manage to apply it on my setText();

I've googled alot and it feels like im really close to success.

The code i got :

if(tag.equals("Customer")) { String name = xpp.getAttributeValue(null, separated_nodes[0].trim()); String number = xpp.getAttributeValue(null, separated_nodes[1].trim()); String SSNumber = xpp.getAttributeValue(null, separated_nodes[2].trim()); String Address = xpp.getAttributeValue(null, separated_nodes[3].trim()); String Postcode = xpp.getAttributeValue(null, separated_nodes[4].trim()); String City = xpp.getAttributeValue(null, separated_nodes[5].trim()); String Phone = "Phone#: " + xpp.getAttributeValue(null, separated_nodes[6].trim()); String Cell = xpp.getAttributeValue(null, separated_nodes[7].trim()); String Email = xpp.getAttributeValue(null, separated_nodes[8].trim()); // text.setText("Network "+xpp.getAttributeValue(null, "Name")); Pattern pattern = Pattern.compile("[0]{1}[0-9]{6,15}"); Linkify.addLinks(text, pattern, "Phone#: "); //Linkify.addLinks(text, pattern, xmlstring); //Linkify.addLinks(text, pattern, Phone); text.setText("Customer: \nName: " + name +"\n" + "Customer Number: "+ number + "\n" + "Social Security Number: "+ SSNumber +"\n" + "Address: "+ Address +"\n" + "Postal Code: "+ Postcode +"\n" + "City: "+ City +"\n" + ""+ Phone +"\n" + "Cellphone#: "+ Cell +"\n" + "e-mail: "+ Email +"\n"); Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES) ; }

As you see, ive tried multiple ways to linkify Phone and Cellphone-number.

I think the RegEx is correct.

Answer1:

<ol><li>you need to call Linkify.addLinks() AFTER you have set up your text view, you're doing it before</li> <li>Doesn't Linkify already support phone numbers, e.g. Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS)?</li> </ol>

<em>Update</em>

Also, in here: Linkify.addLinks(text, pattern, "Phone#: "); the third argument is supposed to be the Scheme, "Phone#:" is NOT a valid scheme. It should be tel:.

Answer2:

Unfortunately, the call to

Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES)

on the last line is wiping out the Spannables that previous calls to Linkify.addLinks() created. See the <a href="http://developer.android.com/reference/android/text/util/Linkify.html#addLinks%28android.text.Spannable,%20int%29" rel="nofollow">Linkify Documentation</a>

Recommend

  • Fetching google images using htmlagilitypack
  • System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary: DY
  • DotnetOpenAuth : No OpenId End point found error for localhost
  • How do I specify a pattern constraint using JAXB annotations?
  • Pkcs11Exception: Method C_Initialize returned 2147483907
  • PHP code to check country from IP
  • How to handle epic long text at y-axis for angular-chart.js?
  • Java Scanner Take Input, call a method, then back to read more input not working
  • php: best way to validate POST
  • List all files in a folder and subfolders in Excel
  • How to access the element at variable index of a tuple?
  • Remote Process Execution
  • Selecting multiple elements with Selenium
  • How do I make WIF use RSA15 when encrypting data using a certificate?
  • C++ exception safety paranoia: how much is too much?
  • How to read xlsx file in an android application?
  • JPA - getting distinct value from one column
  • php regex remove digits
  • Add delivery info to query in SAP Crystal Reports
  • APNs messages are delivered but not received on iOS device
  • C# “cannot assign field because it is a foreach iteration variable”
  • Update a record where _id = :id with Mongoose
  • Excel 2007: Format of email address from Outlook 2007
  • Updating and removing unique join relationships in CakePHP
  • Eloquent Relationship on Partial Match
  • Negating Regex PO BOX
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • converting text file into xml using php?
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • Is there a javascript serializer for JSON.Net?
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Where to put my custom functions in Wordpress?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • using HTMLImports.whenReady not working in chrome
  • LevelDB C iterator
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?