Android phone number mask for EditText

Oleg Ryabtsev picture Oleg Ryabtsev · Feb 26, 2016 · Viewed 15.6k times · Source

I'm developing application for Android. In this app user needs sign up and he needs type phone number. I want make mask for this text field in format like +7 (999) 999-99-99. I've tried use mPhoneNumberEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); but it provides only (999) 999-9999 format. How can I do format which I need?

Answer

Slava picture Slava · Mar 7, 2017

The most effective way to use a mask on EditText in your Android programs in Android Studio is to use MaskedEditText library (GitHub link). It's a kind of custom EditText with Watcher that allows you to set a hint with different color (if you want if will be available even when user already started to type), mask and it's very easy to use :-)

compile 'ru.egslava:MaskedEditText:1.0.5'

<br.com.sapereaude.maskedEditText.MaskedEditText
    android:id="@+id/phone_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:typeface="monospace"
    mask:allowed_chars="1234567890"
    mask:mask="+7(###)###-##-##"
    app:keep_hint="true"
    />

And that is!

enter image description here