3

I'm trying to implement emojis in a way that I automatically replace things like ":)" with their emoji equivalent. And I'm not really sure how to do that, I found a table of emoji UTF codes, but I'm not sure on how I'm supposed to programmatically put them into a EditText :/

 inputField=(EditText)temp.findViewById(R.id.inputField);
    inputField.addTextChangedListener(new TextWatcher() {
        boolean justChanged=false;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(justChanged) {
                justChanged=false;
                return;
            }
            Log.d(s.toString(), s.toString());
            if(s.toString().contains(":)")) {
                justChanged=true;
               inputField.setText(s.toString().replace(":)", "UTF CODE HERE?"));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
3
  • If your current approach listed here doesn't work explain what it does wrong. Commented Feb 25, 2016 at 16:08
  • 1
    It's not that it "doesn't work", I just can't figure out on how to put emoji codes into a String variable :/ I need an example, because just pasting it in there gives that exact code as plain text Commented Feb 25, 2016 at 16:10
  • Have you tried this: stackoverflow.com/a/8854437/1510063 ? Commented Feb 25, 2016 at 16:12

1 Answer 1

3

The simplest way is to achieve this is to encode your source code as UTF-8, and paste your desired emoji into the code. You will then need to pass -encoding UTF-8 to javac. The emoji will then be converted to its Unicode point on compilation.

E.g.

inputField.setText(s.toString().replace(":)", "😁"));

Alternatively, you can use UTF-16 Unicode point literals within Java strings, using the \uXXXX notation. From a suitable Unicode reference site, such as, http://www.fileformat.info/info/unicode/block/emoticons/list.htm, you can get the UTF-16 type encoding or the complete Java escape sequence.

E.g.

inputField.setText(s.toString().replace(":)", "\uD83D\uDE00"));
Sign up to request clarification or add additional context in comments.

3 Comments

the link you provided is not the complete list of emojis..,isn't it?
I tried the code above, does not work. Just sends out : "\uD83D\uDE00" instead of ":)" This absolutely does not answer the question at all. It does no good if the recipient gets a string of this: "\uD83D\uDE00". There is a serious lack of clarity and openness surrounding emojis. This shouldn't be difficult, we are not trying to change the look of the emoji so why can't this be done?
What do you mean "it just sends out"? The answer shows two ways of putting emojis in the source code. Which one are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.