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

0 votes

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 in Others by nisha
• 2,160 points
32 views

1 answer to this question.

0 votes

 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 by polo
• 1,500 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 in Others by Rahul
• 8,980 points
41 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
253 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
379 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,360 points
348 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,360 points
163 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,360 points
207 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,360 points
211 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,360 points
331 views
0 votes
1 answer

extract audio in MP3 format from video in android

Use FFMPEG for android U need to implement a ...READ MORE

answered Jun 16 in Others by polo
• 1,500 points
14 views
0 votes
1 answer

How can I open a URL in Android's web browser from my application?

ry this: Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri ...READ MORE

answered Jun 14 in Others by polo
• 1,500 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP