Skip to content Skip to sidebar Skip to footer

Mask An Edittext With Phone Number Format Nan Like In Phonenumberutils

I want to make user inputted phone number in an editText to dynamically change format every time the user inputs a number. That is, when user inputs up to 4 digits, like 7144, the

Solution 1:

Easiest way to do this is to use the built in Android PhoneNumberFormattingTextWatcher.

So basically you get your EditText in code and set your text watcher like this...

EditTextinputField= (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(newPhoneNumberFormattingTextWatcher());

Nice thing about using PhoneNumberFormattingTextWatcher is that it will format your number entry correctly based on your locale.

Solution 2:

Above answer is right but it works with country specific. if anyone want such formatted phone number(###-###-####). Then use this:

etPhoneNumber.addTextChangedListener(newTextWatcher() {
                @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
                    intdigits= etPhoneNumber.getText().toString().length();
                    if (digits > 1)
                        lastChar = etPhoneNumber.getText().toString().substring(digits-1);
                }

                @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
                    intdigits= etPhoneNumber.getText().toString().length();
                    Log.d("LENGTH",""+digits);
                    if (!lastChar.equals("-")) {
                        if (digits == 3 || digits == 7) {
                            etPhoneNumber.append("-");
                        }
                    }
                }

                @OverridepublicvoidafterTextChanged(Editable s) {

                }
            });

Declare String lastChar = " " in your activity.

Now add this line in xml of your edittext

android:inputType="phone"

That's all.

Edited: If you want your edittext lenght to limit 10 digits add line below also:

android:maxLength="12"

(It is 12 because "-" will take space two times)

Solution 3:

Just add the following to EditText for Phone Number to get a formatted phone number(###-###-####)

Phone.addTextChangedListener(newTextWatcher() {

        intlength_before=0;

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
            length_before = s.length();
        }

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {

        }

        @OverridepublicvoidafterTextChanged(Editable s) {
            if (length_before < s.length()) {
                if (s.length() == 3 || s.length() == 7)
                    s.append("-");
                if (s.length() > 3) {
                    if (Character.isDigit(s.charAt(3)))
                        s.insert(3, "-");
                }
                if (s.length() > 7) {
                    if (Character.isDigit(s.charAt(7)))
                        s.insert(7, "-");
                }
            }
        }
    });

Solution 4:

My script, example taken from here description here


<android.support.design.widget.TextInputLayout
    android:id="@+id/numphone_layout"
    app:hintTextAppearance="@style/MyHintText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_marginTop="8dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_text_numphone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/MyEditText"
        android:digits="+() 1234567890-"
        android:hint="@string/hint_numphone"
        android:inputType="phone"
        android:maxLength="17"
        android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 TextInputEditTextphone= (TextInputEditText) findViewById(R.id.edit_text_numphone);
 //Add to mask
    phone.addTextChangedListener(textWatcher);
}


   TextWatchertextWatcher=newTextWatcher() {
    privateboolean mFormatting; // this is a flag which prevents the  stack overflow.privateint mAfter;

    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
        // nothing to do here..
    }

    //called before the text is changed...@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
        //nothing to do here...
        mAfter  =   after; // flag to detect backspace..

    }

    @OverridepublicvoidafterTextChanged(Editable s) {
        // Make sure to ignore calls to afterTextChanged caused by the work done belowif (!mFormatting) {
            mFormatting = true;
            // using US or RU formatting...if(mAfter!=0) // in case back space ain't clicked...
            {
                Stringnum=s.toString();
                Stringdata= PhoneNumberUtils.formatNumber(num, "RU");
                if(data!=null)
                {
                    s.clear();
                    s.append(data);
                    Log.i("Number", data);//8 (999) 123-45-67 or +7 999 123-45-67
                }

            }
            mFormatting = false;
        }
    }
};

Solution 5:

The above solutions do not take backspace into consideration so when you delete some numbers after typing, the format tends to mess up. Below code corrects this issue.

phoneNumberEditText.addTextChangedListener(newTextWatcher() {

        int beforeLength;

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
            beforeLength = phoneNumberEditText.length();
        }

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            intdigits= phoneNumberEditText.getText().toString().length();
            if (beforeLength < digits && (digits == 3 || digits == 7)) {
                phoneNumberEditText.append("-");
            }
        }

        @OverridepublicvoidafterTextChanged(Editable s) { }
    });

Post a Comment for "Mask An Edittext With Phone Number Format Nan Like In Phonenumberutils"