Reputation: 863
I am developing an application where I want to set phone numbers in the US format, like xxx-xxx-xxxx
. How can I do this?
Upvotes: 1
Views: 6016
Reputation: 13
mobileEditText.addTextChangedListener(watcher); The use the below function..
private final TextWatcher watcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
mAfter = after;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s) {
if (!mFormatting) {
mFormatting = true;
if(mAfter!=0) // in case back space ain't clicked...
PhoneNumberUtils.formatNumber(s,PhoneNumberUtils.getFormatTypeForLocale(Locale.US));
mFormatting = false;
}
Upvotes: 0
Reputation: 2987
try to implement the following code then u will get " - " at entering number
text.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean flag = true;
String eachBlock[] = text.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++)
{
if (eachBlock[i].length() > 3)
{
Log.v("11111111111111111111","cc"+flag + eachBlock[i].length());
}
}
if (flag) {
text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDel = 1;
return false;
}
});
if (keyDel == 0) {
if (((text.getText().length() + 1) % 4) == 0)
{
if (text.getText().toString().split("-").length <= 2)
{
text.setText(text.getText() + "-");
text.setSelection(text.getText().length());
}
}
a = text.getText().toString();
}
else
{
a = text.getText().toString();
keyDel = 0;
}
} else {
text.setText(a);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
public void afterTextChanged(Editable s) {
}
});
Upvotes: 6
Reputation: 6139
http://code.google.com/p/libphonenumber/
Try this for dealing with phone numbers. It support formatting a national/international phone number, like you require.
Upvotes: 1
Reputation: 1504
You'll need to change locale (Settings -> Locale and Text -> Select locale)
otherwise Hope this link will be helpful
http://androidforums.com/g1-support/39788-contact-numbers-us-format-uk.html
Upvotes: 1