12

I have a chat application which offers the possibility to add emoticons to the text.

I have a problem with the EditText field. The emoticon images show up but if I press on the normal keyboard the backspace button, the text which I was changing to an emoticons picture shows up and I have to remove several characters until the picture goes away. I am using Spannable to do this.

I want the whole smilie go away if the user presses one time backspace.

Here the code I am using:

// This is in the keyclicked listener
{
    ...
    smilie = "(angel)";
    break;
    ...
    int cursorPosition = content.getSelectionStart();
    content.getText().insert(cursorPosition, getSmiledText(this, smilie));
    content.getText().insert(cursorPosition + smilie.length(), " ");
}

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Integer> entry : smilies.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(),
                    matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()),
                        matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

public static Spannable getSmiledText(Context context, CharSequence text) {
    Spannable spannable = spannableFactory.newSpannable(text);
    addSmiles(context, spannable);
    return spannable;
}
Onik
  • 19,396
  • 14
  • 68
  • 91
tobias
  • 2,322
  • 3
  • 33
  • 53
  • Off the cuff, subclass `EditText`, override the key management methods, and add in your own backspace-on-an-emoticon logic, chaining to the superclass for all other scenarios. – CommonsWare Jun 22 '13 at 13:31
  • see this http://stackoverflow.com/questions/16876871/convert-drawable-to-a-specific-string/16881037#16881037 – pskink Jun 22 '13 at 13:57
  • Hi tobias. I am facing the same issue. Could you solve that? – Nadir Novruzov Sep 01 '14 at 06:36
  • If anyone is still searching for an answer, [here it is](http://stackoverflow.com/a/19649371/2128979) – Ashish Tanna Nov 14 '15 at 01:23

1 Answers1

2

So you can't force the keyboard to do that- the keyboard generally isn't looking at that kind of info. What you can do is place a TextWatcher on the edit field and override afterTextChanged to detect this case and delete the additional characters needed. It will be a pain but its doable.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127