5

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);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/16768930/implementations-of-emoji-emoticon-view-keyboard-layouts – Etienne Lawlor May 27 '13 at 16:51

2 Answers2

8

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()));
amity
  • 942
  • 1
  • 8
  • 24
  • This Code is Not Get Smileys If i use Your Code the text "obj" is Return so this is not useful for me. – Dipak Keshariya Sep 19 '11 at 12:21
  • @dipak:: but i got perfect text for setting smiley in text view. and one more thing i have try to use your code several time before this post but i can not got the solution.now i am not using ImageGetter method instead of that i am using "addTextChangedListener" and changeing smiley code with image by calling straight forward "getSmiledText" function above. so please try again, it may help you – amity Sep 19 '11 at 12:46
  • I used Your Code in My Project but this does not help me. thanks for help. – Dipak Keshariya Sep 19 '11 at 13:13
0

Use Html.toHtml(Spanned text)

like:

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

edit: iam digging in the dark here but could it be you want the text(string) representation of your Smillie?

like you have:

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

and you want:

String cs = ":)";

is that right? if not, my previous answer gives you the technical String representation of your Html code

Michele
  • 6,126
  • 2
  • 41
  • 45