How to get text and emoticon from Edittext to String?

How to get text and emoticon from edittext to String?

Using the following code, I added Smiley / Emojis to Edittext, but how to get text / emoticon from edittext in 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);
+5
source share
2 answers

Use the 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;
        }

for emotion 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()));
+8
source

Use Html.toHtml (Spanned text)

as:

String myString = Html.toHtml(cs);
System.out.println(myString);

edit: iam is digging in the dark here, but maybe you need a text (string) representation of your Smillie?

like you:

cs = Html.fromHtml(
            "<img src='"
                    + getResources()
                            .getDrawable(R.drawable.happy)
                    + "'/>", imageGetter, null);

and you want:

String cs = ":)";

it is right? if not, my previous answer gives you a technical string representation of your HTML code.

0

All Articles