23

Is there a way I can specify an input mask to the EditText control in Android?

I want be able to specify something like ### - ## - #### for a Social Security Number. This will cause any invalid input to be rejected automatically (example, I type alphabetical characters instead of numeric digits).

I realize that I can add an OnKeyListener and manually check for validity. But this is tedious and I will have to handle various edge cases.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Buzzy
  • 3,618
  • 4
  • 30
  • 41
  • What i understood is you want to specify the input type such as number, characters, password(type)? In that case you can use the method setInputType(int type) also look into http://developer.android.com/reference/android/text/InputType.html or else you can specify in XML while declaring the edit text – Vamsi May 26 '10 at 13:21
  • 1
    I did take a look at those types. I need more control over the format of the text being entered. – Buzzy May 26 '10 at 13:27
  • try this library: https://android.jlelse.eu/edittext-picker-library-4c71ae7d7863 – Ali Azaz Alam May 05 '20 at 12:45

6 Answers6

25

Try using an InputFilter rather than an OnKeyListener. This means you don't have worry about tracking individual key presses and it will also handle things like pasting into a field which would be painful to handle with an OnKeyListener.

You could have a look at the source of the InputFilter implementations that come with Android to give you a starting point for writing your own.

Adam Peck
  • 6,930
  • 3
  • 23
  • 27
David Webb
  • 190,537
  • 57
  • 313
  • 299
8

The easiest way I know 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 it 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="1234567"
    mask:mask="###-##-##"
    app:keep_hint="true"
    />

And that is!

enter image description here

Slava
  • 1,314
  • 14
  • 28
3

You could take a look at the android.telephony.PhoneNumberFormattingTextWatcher class. It masks a phone number text input with the ###-###-#### pattern.

1
String phoneNumber = "1234567890";

String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890

editTextMobile.setText(text); //set editText text equal to new formatted number

editTextMobile.setSelection(text.length()); //move cursor to end of editText view

Use this in an onKey function for delicious on-the-fly bunny sex with formatted phone numbers.

sth
  • 222,467
  • 53
  • 283
  • 367
aaa
  • 35
  • 1
0

This site gives a good 2 classes that help with masking that I found quite useful.

You need to use the items in the comments to improve it. But worth adding to your program for ease of masking your EditText views for many masks.

SatanEnglish
  • 1,346
  • 1
  • 13
  • 22
0

you can use this efect in EditText

enter image description here

Community
  • 1
  • 1
David Hackro
  • 3,652
  • 6
  • 41
  • 61
  • 1
    International phone numbers don't have a single-matching pattern. This method will cause more headache than beating a solution – Farid Aug 06 '21 at 07:22