0

I have this EditText where the user enter a credit card number. I add a blank space every 4 characters by appending to the EditText like so:

cardNumberEditText.append(" ");

I only want to show the numeric keyboard. Therefore I add this attribute to my EditText in XML:

android:inputType="number"

However this doesn't allow me to append anything to the EditText apart from numbers.

I could change the XML attribute to:

android:inputType="number|text"

but this would change the keyboard layout thus allowing the user to also enter text which I don't want.

The "add new payment method" in the Play Store app does what I'm after but I just can't figure out how they do it.

Isak
  • 1
  • 1
  • try [this stackoverflow answer](http://stackoverflow.com/a/21679833/2900127) or [TextWatcher](http://developer.android.com/reference/android/text/TextWatcher.html) – 7geeky Aug 03 '16 at 14:34
  • Try using android:digit="0123456789 _" in conjunction with android:inputType="number". – MGM_Squared Aug 03 '16 at 14:36

1 Answers1

3

When you use inputType="number" it disables " " and "-" chars. You can keep using this type and specify that the " " char should be counted as a digit by specifying digits="0123456789 " in the layout xml as well.

SeanKelleyx
  • 1,155
  • 13
  • 15