Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect emoticons in EditText in android

I want to detect whether my EditText contains smilie (emoticons) or not. But I have no idea that how to detect them.

like image 644
Vivek Chahar Avatar asked Jun 25 '15 05:06

Vivek Chahar


2 Answers

To disable emoji characters when typing on the keyboard I using the following filter:

InputFilter filter = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            int type = Character.getType(source.charAt(i));
            //System.out.println("Type : " + type);
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                return "";
            }
        }
        return null;
    }
};

mMessageEditText.setFilters(new InputFilter[]{filter});

If you need only detect if EditText contains any emoji character you can use this priciple (Character.getType()) in android.text.TextWatcher interface implementation (in onTextChange() or afterTextChanged() method) or e.g. use simple for cycle on mMessageEditText.getText() (returns CharSequence class) with charAt() method.

like image 192
Petr Daňa Avatar answered Oct 09 '22 04:10

Petr Daňa


If by simile you are referring to the figure of speech, you can use .getText() and the String method .contains(String) to check whether it contains the Strings "like" or "as".

Snippet:

EditText myEditText = (EditText)findViewById(R.id.myEditText);
String input = myEditText.getText();
if(input.contains("like") || input.contains("as"))
{
    //code
}
like image 23
Evan Ogra Avatar answered Oct 09 '22 04:10

Evan Ogra