EditText input with pattern android

13,130

Solution 1

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

Solution 2

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-"/>
Share:
13,130
Shardul
Author by

Shardul

Updated on June 19, 2022

Comments

  • Shardul
    Shardul almost 2 years

    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 ?

  • Taras Vovkovych
    Taras Vovkovych about 7 years
    Elegant solution. Thanks
  • Chintan Shah
    Chintan Shah over 6 years
    It didn't follow pattern when I tried to delete characters at random intermediate position and then continued to add new characters from that position.