13

I must be doing something obvious, but I can't figure out what it is. I'm simply trying to insert a character into an Editable:

@Override
public void afterTextChanged(Editable s) {
    Log.d(TAG, "inserting space at " + location);
    s.insert(location, " ");
    Log.d(TAG, "new word: '" + s + "'");
}

But s never changes. The string 's' is long enough, because I print it and it looks good. If I call Editable.clear(), it is cleared, and I can replace multiple characters with Editable.replace(). Ideas?

Sam
  • 7,252
  • 16
  • 46
  • 65
Shawn Lauzon
  • 6,234
  • 4
  • 35
  • 46

4 Answers4

33

I found the problem; I set the inputType as "number" and so adding the space silently failed.

Shawn Lauzon
  • 6,234
  • 4
  • 35
  • 46
  • Is there any way to add a space to an EditText with an inputType of "number" ? – Anubian Noob Aug 04 '15 at 21:36
  • Yes, see my answer below; it involves temporarily clearing the input filters. – BeccaP Nov 13 '15 at 02:23
  • 1
    Instead of `number` you could use `android:inputType="phone"`. It displays the numbers, but with small letters next to them. I'd say normal users won't recognize the difference. – Syex Aug 09 '16 at 14:22
14

To edit an editable with input filters, simply save the current filters, clear them, edit your text, and then restore the filters.

Here is some sample code that worked for me:

@Override
public void afterTextChanged(Editable s) {
    InputFilter[] filters = s.getFilters(); // save filters
    s.setFilters(new InputFilter[] {});     // clear filters
    s.insert(location, " ");                // edit text
    s.setFilters(filters);                  // restore filters
}
BeccaP
  • 1,452
  • 1
  • 13
  • 19
4

My situation was, I want to insert a '-' at third place while typing the zip-code. (Eg. 100-0001). No other character not allowed to enter. I set my EditText in xml,

<EditText
 android:id="@+id/etPostalCode"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:imeOptions="actionDone"
 android:inputType="number"
 android:digits="0,1,2,3,4,5,6,7,8,9,-"
 android:singleLine="true"
 android:maxLength="8"/>

And in my code i add text change listener

etPostalCode.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().contains("-") && s.length() > 3) {
                s.insert(3, "-");
            }
        }
    });

By this way i solved my problem... Please suggest me other ways if there another better options available...

Arun PK
  • 119
  • 1
  • 8
1

Try:

Editable s = getLatestEditable();
Log.d(TAG, "inserting space at " + location);
s.insert(location, " ");
Log.d(TAG, "new word: '" + s + "'");
kohlehydrat
  • 503
  • 1
  • 3
  • 19
  • Sorry no, getEditable() is my own method. Actually this is a callback from TextWatcher.afterTextChanged(Editable s) – Shawn Lauzon Feb 07 '11 at 18:34