Convert Drawable To A Specific String
I have a chat app which I want to extend with emoticons. This code is used to insert a smilie in the text: Spanned cs = Html.fromHtml('
', imageGet
Solution 1:
if you want to replace your emoticons try this:
EditTextet=newEditText(this);
et.setTextSize(24);
et.setHint("this view shows \":)\" as an emoticon, try to type \":)\" somewhere");
finalBitmapsmile= BitmapFactory.decodeResource(getResources(), R.drawable.emo_im_happy);
finalPatternpattern= Pattern.compile(":\\)");
TextWatcherwatcher=newTextWatcher() {
booleanfastReplace=true;
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
//Log.d(TAG, "onTextChanged " + start + " " + before + " " + count);if (fastReplace) {
if (start > 0 && count > 0) {
Stringsub= s.subSequence(start - 1, start + 1).toString();
if (sub.equals(":)")) {
Spannablespannable= (Spannable) s;
ImageSpansmileSpan=newImageSpan(smile);
spannable.setSpan(smileSpan, start-1, start+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
} else {
Spannablespannable= (Spannable) s;
Matchermatcher= pattern.matcher(s);
while (matcher.find()) {
intmstart= matcher.start();
intmend= matcher.end();
ImageSpan[] spans = spannable.getSpans(mstart, mend, ImageSpan.class);
Log.d(TAG, "onTextChanged " + mstart + " " + mend + " " + spans.length);
if (spans.length == 0) {
ImageSpansmileSpan=newImageSpan(smile);
spannable.setSpan(smileSpan, mstart, mend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
Log.d(TAG, "onTextChanged " + s);
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidafterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged " + s);
}
};
et.addTextChangedListener(watcher );
setContentView(et);
here if fastReplace == true
you don't have to scan the whole text but it's only minimal implementation: works only if you type ")" right after typed ":", if fastReplace == false
it replaces every occurrence of ":)" with a smiley but it has to scan the whole text so it's a bit slower when text is quite large
Post a Comment for "Convert Drawable To A Specific String"