thedeepfield thedeepfield - 10 months ago 77
Android Question

Android: Format EditText view for phone numbers

I have an EditText view and I want it to format the users input into phone number format. For example, when the user types in 1234567890, the EditText view should dynamically show it as "(123) 456-7890" as soon as the first 3 numbers are inputted. I tried the following in my OnCreate but it didn't seem to do anything for me...

EditText ET = (EditText) findViewById(;
ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

How can I get the users input to display in phone number format?

Answer Source

I did a combination of things, i modified my input type to phone, then I used regex to remove all non numeric characters: phonenumber = phonenumber.replaceAll("\D", "");