Xml:
<edittext
Android:id= "@+id/edit_main"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:hint= "Please enter your phone number"
Android:inputtype= "Number"
Android:maxlength= "/>"
Activity:
Medittext = (EditText) Findviewbyid (R.id.edit_main);
Medittext.addtextchangedlistener (New Textwatcher () {
@Override
public void beforetextchanged (Charsequence s, int. start, int count, int after) {
}
@Override
public void ontextchanged (charsequence charsequence, int start, int before, int count) {
if (charsequence = = NULL | | charsequence.length () = = 0)
Return
StringBuilder StringBuilder = new StringBuilder ();
for (int i = 0; i < charsequence.length (); i++) {
if (i! = 3 && I! = 8 && Charsequence.charat (i) = = ") {
Continue
} else {
Stringbuilder.append (Charsequence.charat (i));
if ((stringbuilder.length () = = 4 | | stringbuilder.length () = = 9)
&& Stringbuilder.charat (Stringbuilder.length ()-1)! = ") {
Stringbuilder.insert (Stringbuilder.length ()-1, ');
}
}
}
if (!stringbuilder.tostring (). Equals (Charsequence.tostring ())) {
int index = start + 1;
if (Stringbuilder.charat (start) = = ") {
if (before = = 0) {
index++;
} else {
index--;
}
} else {
if (before = = 1) {
index--;
}
}
Medittext.settext (Stringbuilder.tostring ());
Medittext.setselection (index);
}
}
@Override
public void aftertextchanged (Editable s) {
}
});
EditText Enter phone number automatically with space