I put this code from answer Displaying emoticons in Android for showing emoticons in Edittext but I have a problem when I’m typing a lot of text, it becomes slower and slower and almost impossible to type.
This is the code: How can I make it faster?
textS.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
addSmiledText(Main.this, s);
// Log.e("",s.toString());
}
});}
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.face_smile);
emoticons.put(":-)", R.drawable.face_smile);
emoticons.put(":(", R.drawable.face_sad);
emoticons.put(":-(", R.drawable.face_sad);
emoticons.put(":-D", R.drawable.face_smile_big);
emoticons.put(":D", R.drawable.face_smile_big);
emoticons.put(":lol:", R.drawable.face_laughing);
emoticons.put("8)", R.drawable.face_cool);
emoticons.put(";)", R.drawable.face_wink);
emoticons.put(";-)", R.drawable.face_wink);
emoticons.put(";(", R.drawable.face_crying);
...
}
public static Spannable addSmiledText(Context ch, Editable s) {
int index;
for (index = 0; index < s.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > s.length())
continue;
if (s.subSequence(index, index + length).toString().equals(entry.getKey())) {
s.setSpan(new ImageSpan(ch, entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return s;
}
You have:
If you do not want to scan the entire string each time, choose a better starting value for
index.For example, since your minimum-length emoticon has two characters and your maximum-length emoticon has five characters, skip the loop entirely for
Editableobjects of length 0 or 1, and then haveindexstart ats.length()-5.Note, though, that
afterTextChanged()is not only called for changes at the end. The user might have repositioned the cursor to be somewhere in the middle. Hence, you really should be scanning inonTextChanged()and looking at the window of text around the point of the change.