How to Get text and smiley from edittext into String?
Using Following Code I was Add Smiley/Emojis in Edittext but how to get text/smiley from edittext into String Format.
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(
R.drawable.happy);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml(
"<img src='"
+ getResources()
.getDrawable(R.drawable.happy)
+ "'/>", imageGetter, null);
edttxtemoji.setText(cs);
Please use following function.
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
following for emotions code...
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put("8-)", R.drawable.s1);
emoticons.put(":-&", R.drawable.s2);
emoticons.put(">:-)", R.drawable.s3).....};
and settext using
tv_msg_send.setText(getSmiledText(getApplicationContext(), edt_msg.getText().toString()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With