Phone number formatting in Android -


in app, user enters phone number. example, if enters: 123456 want him see: 12 34 56 how can that?

you can add text watcher edit text , check size of text till 2 place , add space in follows

callphoneedittext.addtextchangedlistener(new customtextwatcherforphonenumber()); 

the following class required thing needed

  public static class customtextwatcherforphonenumber implements textwatcher {          // change want... ' ', '-' etc..         private static final char space = ' ';          @override         public void ontextchanged(charsequence s, int start, int before,                 int count) {         }          @override         public void beforetextchanged(charsequence s, int start, int count,                 int after) {         }          @override         public void aftertextchanged(editable s) {             // remove spacing char             if (s.length() > 0 && (s.length() % 5) == 0) {                 final char c = s.charat(s.length() - 1);                 if (space == c) {                     s.delete(s.length() - 1, s.length());                 }             }             // insert char needed.             if (s.length() > 0 && (s.length() % 5) == 0) {                 char c = s.charat(s.length() - 1);                 // if digit there should space insert                 // space                 if (character.isdigit(c)                         && textutils.split(s.tostring(), string.valueof(space)).length <= 2) { ****// @ point check values , add space between them****                     s.insert(s.length() - 1, string.valueof(space));                 }             }         }     } 

check , let me know if helpful or not


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -