8

I am having a EditText in which I have to accept alphanumeric input from user which is specific to pattern, and hyphens '-' are inserted automatically.

"XXX-XXX-XXXX"

how to achieve that ? Is there any pattern tool in android ?

Shardul
  • 786
  • 3
  • 11
  • 20
  • Possible duplicate of [Live editing of users input](http://stackoverflow.com/questions/4172242/live-editing-of-users-input) – bummi Feb 27 '16 at 08:58

2 Answers2

6

You can achieve that with PatternedTextWatcher.

Edit:

EditText editText = (EditText) findViewById(R.id.edittext);

// Add text changed listener for automatic dots insertion.
editText.addTextChangedListener(new PatternedTextWatcher("###-###-####"));

And in XML:

<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:digits="1234567890-"/>
Savelii Zagurskii
  • 997
  • 1
  • 9
  • 9
2

You can use addTextChangedListener to EditText
Refer this question , which demonstrate it

Community
  • 1
  • 1
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112