Reputation: 645
I want to set this format phone no 223-233-3334 for edit text when user typing in keyboard .i know i have to use dialerkeylistener and textfilter .can anybody give sample how to use dialerkeylistener to set format in android
Thanks
Upvotes: 0
Views: 517
Reputation: 2960
For non-NANP (http://www.howtocallabroad.com/nanp.html) countries to implement phone number formatting while user typing, you can use some workaround like this:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String typedNum = yourEditText.getText().toString();
if(typedNum.length() == 3)
{
yourEditText.setText(typedNum + "-");
yourEditText.setSelection(4);
}
else if(typedNum.length() == 7)
{
yourEditText.setText(typedNum + "-");
yourEditText.setSelection(8);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
Otherwise solution can be :
import android.telephony.PhoneNumberFormattingTextWatcher;
OnCreate :
yourEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
If you like to format the number after user has entered the phone number:
import android.telephony.PhoneNumberUtils;
OnCreate :
PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);
Upvotes: 1