How To Create Phone Number Format XXX-XXX-XXXX In Android

+1 vote

Here Below I have mentioned my code.

Declare This under Oncreate

TextView TxtDocPhone=(TextView)findViewById(R.id.TxtDocPhone);

String numbersOnly = keepNumbersOnly(Phone);
String ProviderPhone = formatNumbersAsCode(numbersOnly);

Then call it

 private String keepNumbersOnly(CharSequence s) {
        return s.toString().replaceAll("[^0-9]", ""); 
    }

    private String formatNumbersAsCode(CharSequence s) {
        int groupDigits = 0;
        String tmp = "";
        for (int i = 0; i < s.length(); ++i) {
            tmp += s.charAt(i);
            ++groupDigits;
            if (groupDigits == 3) {
                tmp += "-";
                // tmp += "";
                groupDigits = 0;
            }
        }
        return tmp;
    }

Please guide me the correct way to achieve my objective.

Jun 14, 2022 in Others by nisha
• 2,210 points
9,027 views

1 answer to this question.

–1 vote

 can try:

private String formatNumbersAsCode(CharSequence s) {
    return String.format("%s-%s-%s",s.substring(0,3),s.substring(3,6),s.substring(6));
}
answered Jun 14, 2022 by polo
• 1,480 points

Related Questions In Others

0 votes
1 answer

How to create a Custom Dialog box in android?

Here I have created a simple Dialog, ...READ MORE

answered Feb 18, 2022 in Others by Rahul
• 9,680 points
1,093 views
0 votes
1 answer

How to format numbers as lakhs and crores in excel/ google spreadsheet when the number could be negative too?

Excel formatting, in my opinion, can only ...READ MORE

answered Oct 31, 2022 in Others by narikkadan
• 63,600 points
14,675 views
0 votes
1 answer

How to change the default number format in Excel?

Excel Tips, Learn Excel \ Raghu R Setting ...READ MORE

answered Dec 17, 2022 in Others by narikkadan
• 63,600 points
6,685 views
0 votes
0 answers

How to create a batch file in windows?

What is a batch file and How ...READ MORE

Jul 4, 2019 in Others by sindhu
699 views
0 votes
0 answers

How to create a Wordlist generator in Golang?

I want to create a Wordlist Generator ...READ MORE

Jul 14, 2019 in Others by Raj
• 180 points
880 views
0 votes
1 answer

How to create a Project in Flutter?

Hi@akhtar, Flutter has an inbuilt command to create ...READ MORE

answered Jul 20, 2020 in Others by MD
• 95,460 points
887 views
0 votes
1 answer

How to create subnet inside a VPC in AWS using Terraform?

Hi@akhtar, You need to use aws_subnet resource inside your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
674 views
0 votes
1 answer

How to create a function in Dart language?

Hi@akhtar, There are many function types available in ...READ MORE

answered Jul 22, 2020 in Others by MD
• 95,460 points
749 views
0 votes
1 answer

How to create a Map in Dart Language?

Hi@akhtar, You need to create one variable and ...READ MORE

answered Jul 23, 2020 in Others by MD
• 95,460 points
744 views
0 votes
1 answer

How to create an Icon Button in Flutter?

Hi@akhtar, To create an Icon Button in Flutter, ...READ MORE

answered Jul 24, 2020 in Others by MD
• 95,460 points
1,032 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP