DreamHawk
DreamHawk

Reputation: 785

Linkify Phone-numbers using Regex

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.

Upvotes: 1

Views: 2811

Answers (2)

John Cummings
John Cummings

Reputation: 2184

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 Linkify Documentation

Upvotes: 0

dmon
dmon

Reputation: 30168

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

Update

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:.

Upvotes: 1

Related Questions