Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android insert emoji programmatically

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) {

        }
    });
like image 347
Leo Starić Avatar asked Nov 23 '25 17:11

Leo Starić


1 Answers

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"));
like image 76
Alastair McCormack Avatar answered Nov 25 '25 06:11

Alastair McCormack